类模板成员函数没有"重新定义默认参数错误"?

8 c++ compiler-construction templates visual-c++

为什么以下不会出现编译错误?:

// T.h

template<class T> class X
{
public:
    void foo(int a = 42);
};
Run Code Online (Sandbox Code Playgroud)
// Main.cpp

#include "T.h"
#include <iostream>

template<class T> void X<T>::foo(int a = 13)
{
    std::cout << a << std::endl;
}

int main()
{
    X<int> x;
    x.foo();   // prints 42
}
Run Code Online (Sandbox Code Playgroud)

似乎编译器默默地忽略了13.为什么是这样?
很糟糕的是,如果类模板定义在Main.cpp而不是头文件中,我的确会得到默认参数redefinition错误.

现在我知道编译器会抱怨它,如果它只是一个普通的(非模板)函数.

标准对类模板成员函数或函数模板中的默认参数有什么看法?

Ber*_*ron 3

\n

8.3.6 \xc2\xa76出现在类定义之外的成员函数定义中的默认参数将添加到由类定义中的成员函数声明提供的默认参数集中。
\n[示例:

\n
class C {\n    void f(int i = 3);\n    void g(int i, int j = 99);\n};\nvoid C::f(int i = 3) // error: default argument already\n{ }                  // specified in class scope\nvoid C::g(int i = 88, int j) // in this translation unit,\n{ }                          // C::g can be called with no argument\n
Run Code Online (Sandbox Code Playgroud)\n

--结束示例]

\n
\n

根据标准,它应该会给你一个错误。

\n

  • 有趣的是:C++03 与 C++98 不同。@tusbar引用的是C++98文本,而C++03添加了类模板的例外,并添加到段落“类模板的成员函数的默认参数应在成员函数的初始声明中指定”在类模板内。” - 我怀疑这意味着在类模板成员的情况下禁止类外定义中的默认参数,因为任何默认参数都应在类内指定[而不是在类外],但对我来说似乎不清楚(是否禁止重复?)。 (4认同)
  • @tusbar你只引用了该段落的一部分。它在开头说“除了类模板的成员函数,......”。 (3认同)