1

I was reading this tutorial on Templates and when it came to function templates I found this snippet of code and text

template <class myType>
myType GetMax (myType a, myType b) {
return (a>b?a:b);
}

GetMax <int> (x,y);

When the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appearance of myType by the type passed as the actual template parameter (int in this case) and then calls it. This process is automatically performed by the compiler and is invisible to the programmer.

So how does instantiation work out in the case of Class Templates? The documentation there did not throw any light on this topic. Does it happen when

  1. I create the object
  2. Call the function for the first time
  3. Every time I call the function

Can someone please throw some light on this topic

4

1 回答 1

2

当编译器发现您将它用于<int>. (其他一切都是编译器的实现特定的)

实例化在翻译单元中的每个模板参数发生一次。

于 2013-10-14T07:55:18.030 回答