Próbuję użyć dedukcji argumentu szablonu z dziedziczeniem i std::shared_ptr
. Jak widać w przykładowym kodzie poniżej, przekazuję shared_ptr<Derived>
do szablonowej funkcji nie należącej do członka, która powinna wykonać dedukcję argumentu szablonu. Jeśli ręcznie nazwiesz wszystko, wszystko działa, a jeśli pozwolę na to odejście od szablonu, to nie działa. Wydaje się, że wydaje się być tak, jakby kompilator nie mógł określić typu, jednak komunikat o błędzie pokazuje, że tak się stało. Nie jestem pewien, co się tutaj dzieje i byłbym wdzięczny za wszelkie uwagi. (Visual Studio 2010)Problem z odstąpieniem od argumentu std :: shared_ptr, dziedziczenia i szablonu
#include <memory>
template <typename T>
class Base {};
class Derived : public Base<int> {};
template <typename T>
void func(std::shared_ptr<Base<T> > ptr) {};
int main(int argc, char* argv[])
{
std::shared_ptr<Base<int>> myfoo(std::shared_ptr<Derived>(new Derived)); // Compiles
func(myfoo); // Compiles
func<int>(std::shared_ptr<Derived>(new Derived)); // Compiles
func(std::shared_ptr<Derived>(new Derived)); // Doesn't compile. The error message suggests it did deduce the template argument.
return 0;
}
Komunikat o błędzie:
5> error C2664: 'func' : cannot convert parameter 1 from 'std::tr1::shared_ptr<_Ty>' to 'std::tr1::shared_ptr<_Ty>'
5> with
5> [
5> _Ty=Derived
5> ]
5> and
5> [
5> _Ty=Base<int>
5> ]
5> Binding to reference
5> followed by
5> Call to constructor 'std::tr1::shared_ptr<_Ty>::shared_ptr<Derived>(std::tr1::shared_ptr<Derived> &&,void **)'
5> with
5> [
5> _Ty=Base<int>
5> ]
5> c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(1532) : see declaration of 'std::tr1::shared_ptr<_Ty>::shared_ptr'
5> with
5> [
5> _Ty=Base<int>
5> ]
5>
Oto co GCC 4.7.3 ma do powiedzenia na ten temat, FYI: 't.cpp: 9: 6: uwaga: szablon argumentu odliczenie/substytucja nie powiodło się: t.cpp: 16: 47: Uwaga: niedopasowane typy" baza "i 'pochodne' t.cpp: 16: 47: Uwaga: 'std :: shared_ptr ' nie pochodzi od 'std :: shared_ptr >'' –