Thread: template instantiations

  1. #1
    Registered User
    Join Date
    Jan 2007

    template instantiations

    I have the following code:

    void foo()
      Class<int> c1, c2;
    So two variables of Class<int>.

    What happens at the technical level when the compiler sees this code? Does it generate two Class<int> {}; types and only during link time all the Class<int> types are reduced to one? Or does it only do that for variables in different translation units?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Within a single translation unit, there is no more than one class named Class<int>, but can be multiple instances of it.

    Things are less clear cut with programs that have multiple translation units. Theoretically, the "export" keyword in the standard allows separation of class declarations from definitions (and therefore rationalisation of template instantiation). To date, only compilers based on the EDG front-end (most notably Comeau C++) support the "export" keyword. Compiler vendors consider full support of "export" to be prohibitive (the number of man-years to implement that support in the EDG front end was reportedly mind-boggling). Because of this, IIRC, exported templates are slated for complete removal from the next version of the C++ standard.

    Because of that, with multiple translation units, things vary with compiler, compiler settings, and linker.

    As a rough rule, the more sophisticated the compiler and linker, the more likely it is that the types (or, more accurately, their member functions) are rationalised across a program. With less sophisticated compilers and linkers (particularly those that don't work together for such things) the executable may contain an instantiation of each type that is local to, and therefore replicated for, each translation unit in which it is used.

    Some compilers support some (compiler-specific) notion of "explicit template instantiation", which allows the programmer to manage things explicitly.

    Which means, if you care about such things, you need to read your compiler documentation carefully. Quite a few compilers (and, if distributed with the compiler, the linkers) do support some features related to managing template instantation, but most don't enable them by default. Therefore it is necessary to find and understand the relevant settings.
    Last edited by grumpy; 04-26-2010 at 06:28 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference Counting
    By Dae in forum C++ Programming
    Replies: 10
    Last Post: 08-13-2009, 07:34 AM
  2. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM