7
4

2 回答 2

6

原因是在模板推导过程中您没有得到隐式类型转换,它永远不会达到这一点。

考虑:

template <typename T>
struct foo {};

template <typename U>
void bar(foo<U>)
{}

foo<int> f;
bar(f);

对于对 bar 的调用,编译器可以推断出它U是一个int,并实例化该函数。但是,请考虑:

template <typename U>
void bar(foo<const U>)
{}  // note  ^^^^

foo<int> f;
bar(f);

编译器无法U推断出foo参数的类型与参数的类型相匹配。因此,模板实例化失败。转换没有机会发生。

于 2010-04-19T17:56:29.060 回答
1
template <typename U>
void tf(pointer<const float>);

^ The compiler won't match a function call to this function unless you explicitly specify a parameter type at the function call, since you don't use the typename U as a function argument. I suspect you want to do something like:

template <typename U>
void tf(pointer<U>);
于 2010-04-19T17:42:53.283 回答