By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

Template Instantiation

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Brandon Mitchell" <nu*****@yahoo.com> wrote in message
news:10*************@news.supernews.com...
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
Jul 22 '05 #2

P: n/a
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 [i] would like to separate
the specification from the definition. However,
[i] 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.

Jul 22 '05 #3

P: n/a
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.

Jul 22 '05 #4

P: n/a
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

Jul 22 '05 #5

P: n/a
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!
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.