【发布时间】:2021-10-20 20:20:12
【问题描述】:
template 在使用之前不会被实例化,例如,如果我有这个类模板:
template <typename T>
struct Pow{
T operator()(T const& x) const{ return x * x; }
};
void func(Pow<double>); // Pow<double> instantiated here?
void func(Pow<int>){} // Pow<int> instantiated here?
int main(){
Pow<int> pi; // instantiated here?
func(pi); // Pow<int> instantiated here
}
-
那么模板究竟是什么时候被实例化的呢?
-
Pow<int>是在声明func(Pow<int>)时实例化的吗? -
如果我没有在
main()中使用Pow<int>,那么它是否因为在func中用作其参数的类型而被实例化了?
【问题讨论】:
-
模板何时被实例化是一个非常复杂、复杂和微妙的问题。这就是我要说的。
标签: c++ template-instantiation