[当我使用c ++ 11编译以下程序时,出现错误error: cannot add a default template argument to the definition of a member of a class template
。为何编译器不允许成员函数定义中包含默认模板参数?
#include <iostream>
template<typename A, typename T = int>
class my_class
{
private:
void dosomething();
};
template<typename A, typename T = int>
void my_class<A, T>::dosomething()
{
}
int main( )
{
B<int> obj;
return 0;
}
将int从函数定义中删除。即template<typename A, typename T = int>
成为template<typename A, typename T>
默认模板参数是在模板参数中的=之后指定的模板参数(17.3)。默认值可以为任何类型的模板参数(类型,非类型,模板)指定模板参数模板参数包(17.5.3)。可以在模板声明中指定默认模板参数。默认模板参数不应在定义以下内容的template-parameter-lists中指定出现在成员班级之外的班级模板的成员。默认模板参数不能在好友类模板声明中指定。如果朋友功能模板声明指定了默认模板参数,该声明应为定义,并且应为唯一的声明翻译单元中的功能模板。