473,406 Members | 2,377 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,406 software developers and data experts.

template-fn implemented in different object does not link

Hi Ng,

i tried to instantiate a template class in a different compilation unit
than it is implemented. It seems while linking it together the linker
does not call the compiler to generate the missing functions.

Doing it all in one object seems no problem.
How to reproduce:

--------------
scratchpad.hpp
--------------
template < typename T >
class TemplatedBase
{
public:
T member;
int foo(const T arg);
};

--------------
scratchmod.cpp
--------------
#include "scratchpad.hpp"

template < typename T >
int TemplatedBase<T>::foo (const T arg)
{
return 1;
}

--------------
scratchpad.cpp
--------------
#include "scratchpad.hpp"

int main()
{
char c = 42;
TemplatedBase<charp;
p.foo(c);
}

--------------

g++ scratchpad.cpp scratchmod.cpp

/cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
reference to `TemplatedBase<char>::foo(char)'

Is it a bug or a feature? And how to do it right?

Regards,

Michael
Oct 12 '06 #1
2 1311
reppisch wrote:
Hi Ng,

i tried to instantiate a template class in a different compilation unit
than it is implemented. It seems while linking it together the linker
does not call the compiler to generate the missing functions.

Doing it all in one object seems no problem.
How to reproduce:

--------------
scratchpad.hpp
--------------
template < typename T >
class TemplatedBase
{
public:
T member;
int foo(const T arg);
};

--------------
scratchmod.cpp
--------------
#include "scratchpad.hpp"

template < typename T >
int TemplatedBase<T>::foo (const T arg)
{
return 1;
}

--------------
scratchpad.cpp
--------------
#include "scratchpad.hpp"

int main()
{
char c = 42;
TemplatedBase<charp;
p.foo(c);
}

--------------

g++ scratchpad.cpp scratchmod.cpp

/cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
reference to `TemplatedBase<char>::foo(char)'

Is it a bug or a feature? And how to do it right?

See:
http://www.parashift.com/c++-faq-lit...html#faq-35.12

Regards,
Sumit.
Oct 12 '06 #2
>
See:
http://www.parashift.com/c++-faq-lit...html#faq-35.12
Thanks!
I was used to the ibm VA-Compiler which resolved that issue by a
tmpinc-mechanism automatically.
Oct 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xenos | last post by:
The new version of GCC is out and in its list of changes, it talks about the C++ Standard's requirements for using the typename and template keywords to disambiguate dependent names. I'm use to...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
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. ...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
4
by: Howard Gardner | last post by:
// I think that there is no way to write the template "cant_write_me" in // this example. Would love to be wrong. // one of many approaches that won't work template< template< typename class...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.