可有人请帮助我理解为什么下面的代码不会编译:没有投投虽然操作定义
template< typename T >
class A
{};
template< typename U >
class wrapper
{
public:
// cast operator
operator wrapper< A<void> >()
{
return wrapper< A<void> >{};
}
};
template< typename T >
void foo(wrapper< A<T> >)
{}
int main()
{
foo( wrapper<void>{});
}
错误消息:
t.cpp:24:7: error: no matching function for call to 'foo'
foo( wrapper<void>{});
^~~
t.cpp:18:10: note: candidate template ignored: could not match 'A<type-parameter-0-0>' against 'void'
void foo(wrapper< A<T> >)
^
1 error generated.
,以及如何解决它?
我预计wrapper<void>
被铸造到wrapper< A<void >
使用演员运算符class wrapper
。
int main()
{
foo(static_cast< wrapper< A<void> > >(wrapper<void>{}));
}
其中编译顺利:使用static_cast
作为显示在下面的
要修复它执行显式的静态转换,所以模板参数的扣除会得到正确的类型。您似乎认为模板参数扣除会考虑可能的用户定义转换。 – VTT