Keeping the template implementation in .C

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Re: Keeping the template implementation in .C

    Hi there.


    "qazmlp" <qazmlp1209@rediffmail.com> schrieb im Newsbeitrag
    news:db9bbf31.0307270827.a3053f4@posting.google.co m...
    > 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: [url]http://gemini.futurezone.com[/url]



    Rene Eng Guest

  2. Similar Questions and Discussions

    1. web cam implementation
      Hello: Does anyone know of a way to implement a web cam with 30 second interval picture updates? This would be placed in a studio setting (radio)...
    2. MD5 implementation
      I need to use MD5 to generate a hash of a string. It needs to be compatible with the MD5 implementation in PHP. public string ComputeMD5(string...
    3. RSA implementation for PHP
      Hi @ll, Is there a RSA library for php available? Has somebody any samples to decrypt a encrypted message with the public key given in a...
    4. RSA implementation of PHP
      Hi @ll, Is there a RSA library for php available? Has somebody any samples to decrypt a encrypted message with the public key given in a...
    5. NAT implementation on AIX 5.x
      Hi: I have a need to write my own version of NAT for AIX 5.x. I have been able to do this on HP-UX by using a STREAMS module and DLPI approach....
  3. #2

    Default Re: Keeping the template implementation in .C

    > > 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 Guest

  4. #3

    Default Re: Keeping the template implementation in .C

    qazmlp wrote:
    > 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?
    >
    This will work, you'll just have to add explicit instantiations in
    your .C file for the specializations you need, as the compiler can't
    do it for you (without export). You might want to do this if
    your implementation requires you to include other header files;
    this way you only need to include them in your .C file and users of
    your header file will not depend on them.

    Mike
    --
    [url]http://www.lazycplusplus.com/[/url]
    The Lazy C++ Programmer's Tool

    Mike Spencer Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139