Template Instantiation

Discussion in 'C++' started by Brandon Mitchell, Mar 1, 2004.

  1. I have read that it is impossible to define a templated class outside of
    the specification file, due to a limitation in gcc's linker. I'm using
    gcc 3.2.3 (no switches) and would like to seperate the spec from the
    definition, however am getting 'undefined reference' errors between my
    object files. Is there a workaround, patch or other solution to this
    problem, or am I SOL? Any help is greatly appreciated. Thank you!

    Brandon Mitchell
    Brandon Mitchell, Mar 1, 2004
    #1
    1. Advertising

  2. Brandon Mitchell

    Mike Wahler Guest

    "Brandon Mitchell" <> wrote in message
    news:...
    > I have read that it is impossible to define a templated class outside of
    > the specification file, due to a limitation in gcc's linker. I'm using
    > gcc 3.2.3 (no switches) and would like to seperate the spec from the
    > definition, however am getting 'undefined reference' errors between my
    > object files. Is there a workaround, patch or other solution to this
    > problem, or am I SOL? Any help is greatly appreciated. Thank you!


    I know of only two workarounds:

    1. Keep your full template definition in a header.
    2. Use Comeau C++, which supports the 'export' keyword.

    -Mike
    Mike Wahler, Mar 1, 2004
    #2
    1. Advertising

  3. Brandon Mitchell wrote:

    > I have read that it is impossible to define a templated class outside of
    > the specification file, due to a limitation in gcc's linker.
    > I'm using gcc 3.2.3 (no switches) and would like to separate
    > the specification from the definition. However,
    > am getting 'undefined reference' errors between my object files.
    > Is there a workaround, patch or other solution to this problem?
    > Or am I SOL?
    > Any help is greatly appreciated.


    For the time being, the recommended method is to explicitly instantiate
    each required template class (just after the class template definition).

    Type

    info gcc

    and search for Template Instantiation
    to get a complete rundown of your options.
    Also, try posting your question to the gnu.g++.help newsgroup.
    E. Robert Tisdale, Mar 2, 2004
    #3
  4. Brandon Mitchell

    Rolf Magnus Guest

    Brandon Mitchell wrote:

    > I have read that it is impossible to define a templated class outside
    > of the specification file, due to a limitation in gcc's linker.


    Actually, it's a "limitation" in the way C++ compilers/linkers typically
    work. The problem is that you're not producing a template class, but
    rather a class template, i.e. a recipe that tells the compiler how to
    generate the actual class, once it knows the template parameters. So
    the compiler cannot generate code before the template gets intantiated.
    But if the template code is in anoter translation unit that its
    instantiation, how would the compiler know what code to generate?

    > I'm using gcc 3.2.3 (no switches) and would like to seperate the spec
    > from the definition, however am getting 'undefined reference' errors
    > between my object files. Is there a workaround, patch or other
    > solution to this problem, or am I SOL? Any help is greatly
    > appreciated. Thank you!


    #include the file with the definitions at the end of your header.
    Rolf Magnus, Mar 2, 2004
    #4
  5. Brandon Mitchell

    Julie Guest

    gcc isn't part of the C++ standard. You will probably get a better answer to
    your question in some gcc-specific newsgroup such as gnu.gcc.

    Brandon Mitchell wrote:
    >
    > I have read that it is impossible to define a templated class outside of
    > the specification file, due to a limitation in gcc's linker. I'm using
    > gcc 3.2.3 (no switches) and would like to seperate the spec from the
    > definition, however am getting 'undefined reference' errors between my
    > object files. Is there a workaround, patch or other solution to this
    > problem, or am I SOL? Any help is greatly appreciated. Thank you!
    >
    > Brandon Mitchell
    Julie, Mar 2, 2004
    #5
  6. Thank you for your responses. I've been banging my head up against this
    problem for a couple of days now, to now avail. In response to all, if
    what Rolf says is true (and I have no reason to doubt ;^), then I should
    NOT repost to a 'gcc-only' forum, as this question pertains to ALL C++
    compilers, thereby making it a language-wide issue.

    Rolf:
    Thanks for your response in particular. I've been looking for this
    type of explanation for some time now. The only work around I found that
    DID work was to explicity instantiate the class at the end of the header
    file for EACH TYPE I needed! This, as you can imagine, is not the best
    method in terms of code generality. Thanks again for your help!
    Brandon Mitchell, Mar 2, 2004
    #6
    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. Fernando Cuenca
    Replies:
    4
    Views:
    2,508
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    593
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    1
    Views:
    552
    Salt_Peter
    Dec 25, 2006
  4. Ed
    Replies:
    1
    Views:
    336
  5. Noah Roberts
    Replies:
    6
    Views:
    1,122
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page