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错误.
现在我知道编译器会抱怨它,如果它只是一个普通的(非模板)函数.
标准对类模板成员函数或函数模板中的默认参数有什么看法?
\n\n8.3.6 \xc2\xa76出现在类定义之外的成员函数定义中的默认参数将添加到由类定义中的成员函数声明提供的默认参数集中。
\n
\n[示例:Run Code Online (Sandbox Code Playgroud)\nclass 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
--结束示例]
\n
根据标准,它应该会给你一个错误。
\n