Template Instantiation

  • Thread starter Brandon Mitchell
  • Start date
B

Brandon Mitchell

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
 
M

Mike Wahler

Brandon Mitchell said:
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
 
E

E. Robert Tisdale

Brandon said:
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.
 
R

Rolf Magnus

Brandon said:
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.
 
J

Julie

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.
 
B

Brandon Mitchell

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top