Şablon argüman kesintisini kalıtım ve std::shared_ptr
ile kullanmaya çalışıyorum. Aşağıdaki örnek kodda gördüğünüz gibi, şablon argümanı indirimi yapması gereken şablonlu bir işleve shared_ptr<Derived>
aktarıyorum. Eğer el ile yazdığım herşeyi çalışırsa ve şablon argüman indirimi yapmasına izin verip vermezse. gibi görünüyor, derleyici türünü bulamadı, ancak hata mesajı gösterdi. Burada neler olduğundan emin değilim ve herhangi bir girdi için minnettar olurum. (Visual Studio 2010)Std :: shared_ptr, devralma ve template argüman indirgemeyle ilgili sorun
#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;
}
hata iletisi:
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>
İşte GCC 4.7.3 Bilginize, bu konuda söylenecek vardır ne: 't.cpp: 9: 6: not: şablon argümanı kesinti/ikame başarısız oldu: t.cpp: 16: 47: not: uyumsuz türleri ' Temel 've' Türetilmiş ' t.cpp: 16: 47: not:' std :: shared_ptr ',' std :: shared_ptr > '' –