【发布时间】:2012-06-29 04:33:25
【问题描述】:
我有这个代码:
struct A{};
template<class T = A>
struct B {
void foo() {}
};
B b; //Error: missing template arguments before 'b'
//Error: expected ';' before 'b'
//More errors
b.foo()
如果我将foo() 设为具有相同模板“签名”的模板函数,编译器不会抱怨未指定模板参数:
struct A {};
struct B {
template<class T = A>
void foo() {}
};
B b; //OK
b.foo()
那么为什么我需要为具有默认参数的模板类指定参数,而不是为模板函数指定参数?有没有我遗漏的一些微妙之处?
原因肯定是模板参数推导失败。但我想知道为什么。
【问题讨论】:
标签: c++ templates default-parameters template-argument-deduction