Re: Keeping the template implementation in .C

Discussion in 'C++' started by Rene Eng, Jul 27, 2003.

  1. Rene Eng

    Rene Eng Guest

    Hi there.


    "qazmlp" <> schrieb im Newsbeitrag
    news:...
    > It is advised to have the template implementation inside header files.
    > But, Say, I keep the implementation in .C file. Are there any run time
    > problems expected out of this or this is simply a link time problem?
    >


    I'd say it would not work at all!
    It is necessary to compile the whole template code with the type of the
    template parameter known, otherwise the compiler can not generate the
    correct object code and can not check if the template parameter meets all
    requirements (like operators etc.)
    In other words: You can create an object of a template module and then
    use this object code for different instantiations of the template.

    One solution to this problem is to place the definition in the .h file and
    the implementation in the .cpp file, but then you have to include the
    ..cpp file from the .h file, exactly the other way round than it is normally
    done.
    May be confusing if somebody else is looking at the code.


    > Also, I would like to confirm whether having the template
    > implementation in .h will increase the executable size for any reason
    > compared to its size when the implementation is kept in a .C file.


    Since the latter is not possible there is nothing to compare against.
    However, the size of your executable will be larger since each template
    instantiation generates a new class with all the code of the template.


    hth
    René



    =======================================
    C++ sources, cross-platform (UNIX and WinTel) and
    covering several topics: http://gemini.futurezone.com
     
    Rene Eng, Jul 27, 2003
    #1
    1. Advertising

  2. > > It is advised to have the template implementation inside header files.
    > > But, Say, I keep the implementation in .C file. Are there any run time
    > > problems expected out of this or this is simply a link time problem?

    >
    > I'd say it would not work at all!
    > It is necessary to compile the whole template code with the type of the
    > template parameter known, otherwise the compiler can not generate the
    > correct object code and can not check if the template parameter meets all
    > requirements (like operators etc.)


    That is not entirely true, if the compiler supports the export keyword,
    template definitions can be put in a .cpp file. Unfortunately very few
    compilers currently do support this keyword. AFAIK only compilers based on
    the EDG front end support this keyword.

    > In other words: You can create an object of a template module and then
    > use this object code for different instantiations of the template.
    >
    > One solution to this problem is to place the definition in the .h file and
    > the implementation in the .cpp file, but then you have to include the
    > .cpp file from the .h file, exactly the other way round than it is

    normally
    > done.
    > May be confusing if somebody else is looking at the code.
    >
    > > Also, I would like to confirm whether having the template
    > > implementation in .h will increase the executable size for any reason
    > > compared to its size when the implementation is kept in a .C file.

    >
    > Since the latter is not possible there is nothing to compare against.


    Well it is possible, but it is questionable if it will make a big
    difference, since basically every time a template is instantiated code
    specific for that instantiation must be generated. The only potential saving
    I can see is that it might be easier to share code that is independant of
    template parameters.

    > However, the size of your executable will be larger since each template
    > instantiation generates a new class with all the code of the template.


    There are ways to avoid, or at least reduce code bloat caused by template
    classes.

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 27, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Spencer
    Replies:
    0
    Views:
    408
    Mike Spencer
    Jul 29, 2003
  2. er
    Replies:
    1
    Views:
    356
  3. Michael Tsang
    Replies:
    32
    Views:
    1,159
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,241
    Phil Carmody
    Mar 30, 2010
  5. sanket
    Replies:
    7
    Views:
    1,101
    Tsung
    Nov 3, 2011
Loading...

Share This Page