extern templates

This is a discussion on extern templates within the C++ Programming forums, part of the General Programming Boards category; Consider the following code: Class<int> p1, p2; I heard that when a compiler compiles this it instantiates TWO times Class<int> ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    extern templates

    Consider the following code:

    Class<int> p1, p2;

    I heard that when a compiler compiles this it instantiates TWO times Class<int> and let the linker remove all the duplicated instantantiations and that thats why extern templates are introduced.

    Is that true? Or does it just create one instance if it encounters two variables of the same templates type?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    From what I understand, the "double instantiation" stuff only happens when you use the templates in separate .cpp files. More specifically, templates are compiled for each object file produced, since the compiler doesn't have any way of knowing if the template will appear in other object files when they get linked together. The linker does have this sort of information, however, and deals with the duplicates in a polite and quiet manner like you said.


    EDIT: Turns out there's another way of handling it (called the CFront model), take a look at the GCC docs for more info.
    Last edited by bernt; 11-18-2010 at 02:34 PM.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern templates
    By KIBO in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2010, 05:28 AM
  2. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21