我不明白这是为什么不工作:依赖模板的参数类型
template <typename T>
struct TypeWrapper
{
typedef T type;
};
template <>
struct TypeWrapper<char*>
{
typedef std::string type;
};
template <>
struct TypeWrapper<const char*>
{
typedef std::string type;
};
template <int N>
struct TypeWrapper<char[N]>
{
typedef std::string type;
};
template <int N>
struct TypeWrapper<const char[N]>
{
typedef std::string type;
};
class A
{
public:
template< typename T >
A(const typename TypeWrapper<T>::type& t)
{
// do smthing
std::cout << t << std::endl;
}
};
int main(void)
{
A a(42);
return 0;
}
我与Visual Studio 2010的编译,我得到以下错误:
error C2664: 'A::A(const A &)' : cannot convert parameter 1 from 'int' to 'const A &'
如果我改变构造A到这一个它的工作原理:
A(const T& t)
但我想处理char *类型作为std ::字符串和可能其他类型adju stements,whithouth复制构造函数(定义特定构造每种类型,这个工程)
你不能推断出这样的模板参数。这与尝试推断何时使用'std :: vector'相同。 –
chris