473,395 Members | 1,584 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,395 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 1565

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.