Use typedef and use a template instance?

Say I have a template class defined as

template <typename T> class Temp{ // irrelevant }; 

I can either implicitly or explicitly instantiate it:

 Temp<int> ti; template class Temp<char>; 

With an explicit instantiation, my program must contain an instance, even if I will not use it later (suppose that it is not skipped by compiler optimization).

My question is, do the following statements lead to an instance of the class?

 typedef Temp<short> TShort; using TFloat = Temp<float>; // C++11 
+7
c ++ templates
source share
1 answer

Not. Implicit creation occurs only when a fully defined type is required; and the type alias should not.

When a code refers to a template in a context that requires a fully defined type, or when the completeness of the type affects the code, and that particular type has not been explicitly instantiated, implicit creation occurs. For example, when an object of this type is constructed, but not when building a pointer to this type.

eg. The following code requires a fully defined type ,

 Temp<char> tc; new Temp<char>; sizeof(Temp<char>); 

while

 Temp<char>* ptc; 

not.

+4
source share

All Articles