我刚刚阅读了有关CRTP的wiki文章,我对模板实例化有点困惑.
根据维基,
成员函数体(定义)直到它们的声明后很久才被实例化.
我不太明白这意味着什么.
假设我有一个类模板:
template <typename T>
class A
{
public:
void foo(T t)
{
//...
};
};
Run Code Online (Sandbox Code Playgroud)
当我实例化类模板A时,它是否实例化成员函数foo()?
例如:
//in .cpp file
int main()
{
A<int> a; //question 1
//class template is instantiated here, isn't it?
//What about foo(), is it instantiated too?
a.foo(10); //question 2
//according to the quotation, foo() will not be instantiated until it is used.
//if so, foo() is instantiated right here, not in question 1, right?
}
Run Code Online (Sandbox Code Playgroud) 我有一个C++模板类,它使用3个不同的类型参数进行实例化.有一种方法,类只需要对这些类型中的一种进行处理,而且不会使用其他两种类型进行调用.
是否会生成该方法的对象代码三次(对于实例化模板的所有类型),或者只生成一次对象代码(对于实际使用它的类型)?