【发布时间】:2011-11-21 11:18:07
【问题描述】:
我刚刚阅读了关于 CRTP 的 wiki 文章,对模板实例化有点困惑。
根据维基,
成员函数体(定义)直到很长时间才被实例化 在他们的声明之后。
我不太明白这是什么意思。
假设我有一个类模板:
template <typename T>
class A
{
public:
void foo(T t)
{
//...
};
};
当我实例化类模板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?
}
【问题讨论】:
-
Plug:这可能会回答你的问题:stackoverflow.com/questions/7182359/…
标签: c++ templates instantiation