标准 N4296::14.7.1/1
说:
The implicit instantiation of a class template specialization causes the implicit instantiation of the declarations, but not of the definitions, default arguments, or exception-specifications of the class member functions, member classes, scoped member enumerations, static data members and member templates;
然后让我们考虑以下代码:
template <class T>
class A
{
void foo(){ }
};
A<int> a; //Implicit instantiation here
int main(){ }
因此,当模板被隐式实例化时,它实际上是在一个没有任何成员函数的类中实例化的,因为我们没有使用它。是真的吗?
最佳答案
该类有一个已声明但尚未定义的成员函数。
这在非模板类中也是允许的;一个(非虚)成员函数只需要一个定义,如果你使用它。
所以在你的模板中,函数声明在实例化时必须有效,但函数体还不需要工作。
关于c++ - 模板类成员函数初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29263302/