[英]Argument deduction for template member functions does not work for classes declared inside function?
struct Test
{
template <class T>
void print(T& t)
{
t.print();
}
};
struct A
{
void print() {printf( "A");}
};
struct B
{
void print() {printf( "B");}
};
void test_it()
{
A a;
B b;
Test t;
t.print(a);
t.print(b);
}
This compiles fine.这编译得很好。
struct Test
{
template <class T>
void print(T& t)
{
t.print();
}
};
void test_it()
{
struct A
{
void print() {printf( "A");}
};
struct B
{
void print() {printf( "B");}
};
A a;
B b;
Test t;
t.print(a);
t.print(b);
}
This fails with error: no matching function for call to 'Test::print(test_it()::A&)'这失败并出现错误:没有匹配的 function 调用 'Test::print(test_it()::A&)'
Can anyone explain me why this happen?谁能解释我为什么会这样? Thanks!!!
谢谢!!!
In your second example, A
and B
are local types, which can't be used as template type arguments in C++03 as per §14.3.1/2 :在您的第二个示例中,
A
和B
是本地类型,根据§14.3.1/2不能在 C++03 中用作模板类型 arguments :
A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.
本地类型、没有链接的类型、未命名类型或由这些类型中的任何一种组合而成的类型不应用作模板类型参数的模板参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.