#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace std;
struct TestClass
{
static void Create(boost::shared_ptr<const int> shp)
{
cout << "TestClass::Create: " << *shp << endl;
}
template <typename T>
static void CreateT(boost::shared_ptr<const T> shp)
{
cout << "TestClass::CreateT: " << *shp << endl;
}
};
int main()
{
boost::shared_ptr<int> shpInt = boost::make_shared<int>(10);
boost::shared_ptr<const int> shpConstInt = shpInt;
TestClass::Create(shpInt); // OK
TestClass::Create(shpConstInt); // OK
//error C2664: 'void TestClass::CreateT<int>(boost::shared_ptr<T>)' :
//cannot convert parameter 1 from 'boost::shared_ptr<T>' to 'boost::shared_ptr<T>'
TestClass::CreateT(shpInt); // ERROR
TestClass::CreateT(shpConstInt); // OK
// workaround
boost::shared_ptr<const int> shpConstInt2 = shpInt;
TestClass::CreateT(shpConstInt2); // OK
return 0;
}
问题>为什么TestClass::CreateT(shpInt)
不起作用,而TestClass::Create(shpInt)
正常工作。是否因为TestClass::CreateT
是仅支持静态绑定的模板函数,无法自动从boost::shared_ptr<T>
转换为boost::shared_ptr<const T>
?静态非模板成员函数与静态模板成员函数
谢谢
对于'CreateT',它应该尝试转换哪一个(许多'T'可能是可能的)?顺便说一句,'CreateT(shpInt)'应该有效。 –
Jarod42
是的,它在我做出更改后生效。为什么最初的那个不起作用? – q0987
我的意思是:如何boost :: shared_ptr '和'boost :: shared_ptr'有关系吗? (你期望'T == U') –
Jarod42