是否可以在C++中为模板函数的变量定义默认值?
如下所示:
template<class T> T sum(T a, T b, T c=????)
{
return a + b + c;
}
Run Code Online (Sandbox Code Playgroud) 我理解(这是一个来源),只要两个定义不冲突,就可以重新定义默认模板参数。因此,我尝试使用 g++ 5.3.1 编译以下内容:
\n\ntemplate <class = int> class A; // forward declaration\ntemplate <class T = A<>> struct B {};\n\ntemplate <class T = int> class A {}; // "= int" here is for clarity\n\nint main() { return 0; }\n
Run Code Online (Sandbox Code Playgroud)\n\n编译器抱怨:
\n\n\n\n\n错误:重新定义 \xe2\x80\x98class T\xe2\x80\x99 的默认参数
\n
我的理解哪里不对?
\n