Thread: extern templates

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

    extern templates

    I dont understand how I can improve compilation time with 'extern template' which is introduced in C++0x.

    I can just put extern before a template object in a header anbd then use it as normal in the source file? like this:

    Code:
    #include <vector>
    
    // bla.h
    struct bla
    {
      void f();
      extern template std::vector<int> vec;
    };
    
    //bla.cpp
    void bla::f()
    {
      vec.push_back(3);
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Basically.

    The idea is to tell the compiler "Do not fully instantiate this template in this translation unit; it will be instantiated elsewhere.". (There remains some confusion about this when it comes against `inline' methods.) It works almost like "`extern' variable" declarations do for variables.

    You will need to force a full instantiation in a separate translation unit.

    Soma

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I put "extern template" into Google and the very first link gave a detailed explanation. Try it, it really works
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    duh ofcourse i did that but I still dont understand. Take the IBM example.

    Code:
    template<class C> C foo(C c) { return c; }
    
    extern template int foo<int>(int);  // extern explicit template instantiation
    
    int i = foo(1);  // does not cause instantiation of the body of foo<int>
    ok I get this, the template function is not instantiated here.
    But if you dont instantiate it somewhere dont you get a linker error then? Or what do you have to do to get it to compile and link? nothing?

  5. #5
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The short version is, you need to instantiate it somewhere, but not everywhere in your code, so you don't need the linker to sort out all the duplicate instantiations in different source files later.

    This is a direct quote from the first hit in my Google search:

    When the compiler encounters an extern template directive, it suppresses the generation of code for that particular specialization. However, an explicit instantiation of the entity must appear in another translation unit, or later in the same translation unit. An explicit instantiation definition of a given entity shall appear at most once in the program.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  4. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM