473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Template Instantiation

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
5 1560

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
3
by: Patrick Guio | last post by:
Hi, I have trouble to compile the following piece of code with g++3.4 but not with earlier version // Foo.h template<typename T> class Foo { public:
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
3
by: sks | last post by:
Hello all Is the usage of extern keyword valid for telling the compiler to NOT instantiate a template and to link it from an another binary? For example: Suppose module A's binary contains a...
8
by: Ole Nielsby | last post by:
I want to create (with new) and delete a forward declared class. (I'll call them Zorgs here - the real-life Zorks are platform-dependent objects (mutexes, timestamps etc.) used by a...
2
by: aitrob | last post by:
Hi, I have a problem concerning templates/inheritance. I have a code that compiles fine with g++ 4.0.1 (Apple version), but gives a lot of errors with Intel C++ 10.1 (Mac OS X). I'm not sure if...
4
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.