www.digitalmars.com         C & C++   DMDScript  

c++.beta - Template member definitions & translation units

reply Richard <Richard_member pathlink.com> writes:
2.1 and 2.1-8

Problem: template declaration in one source file (.h), template member function
definitions in another source file (unit A), and template member function use in
a third source file (unit B). Linker complains that symbol for template member
function is undefined.

// test.h
template <class T>
struct A {
void fn();
};

// test.cpp
#include "test.h"
template <class T> void A<T>::fn() { }

// main.cpp
#include "test.h"
void main() {
A<int> a;
a.fn();
}

// Error 42: Symbol Undefined
// ?fn ?$A H  QAEXXZ (void syscall A<int >::fn(void ))

This has slowly become a high priority for me.

Richard
Jan 23 2003
next sibling parent reply "Walter" <walter digitalmars.com> writes:
Unfortunately, you'll need to put the template bodies into the header files.
That's the way it is with a separate compilation model. The good part is
that there's no penalty for doing so (other than a bit slower compilation
speed).

"Richard" <Richard_member pathlink.com> wrote in message
news:b0pm9c$3p1$1 digitaldaemon.com...
 2.1 and 2.1-8

 Problem: template declaration in one source file (.h), template member

 definitions in another source file (unit A), and template member function

 a third source file (unit B). Linker complains that symbol for template

 function is undefined.

 // test.h
 template <class T>
 struct A {
 void fn();
 };

 // test.cpp
 #include "test.h"
 template <class T> void A<T>::fn() { }

 // main.cpp
 #include "test.h"
 void main() {
 A<int> a;
 a.fn();
 }

 // Error 42: Symbol Undefined
 // ?fn ?$A H  QAEXXZ (void syscall A<int >::fn(void ))

 This has slowly become a high priority for me.

 Richard

Jan 23 2003
parent reply Richard Grant <fractal clark.net> writes:
In article <b0ptcq$8f3$1 digitaldaemon.com>, Walter says...
Unfortunately, you'll need to put the template bodies into the header files.
That's the way it is with a separate compilation model. The good part is
that there's no penalty for doing so (other than a bit slower compilation
speed).

Ok, that's fine for me, and a non exhaustive search of boost lib code indicates awareness of compilation models, but it does create problems with libs that are unaware.. just to be clear: are you saying that this behavior is by design, and that no change is anticipated? Richard
Jan 24 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Richard Grant" <fractal clark.net> wrote in message
news:b0r5gj$t14$1 digitaldaemon.com...
 In article <b0ptcq$8f3$1 digitaldaemon.com>, Walter says...
Unfortunately, you'll need to put the template bodies into the header


That's the way it is with a separate compilation model. The good part is
that there's no penalty for doing so (other than a bit slower compilation
speed).


 awareness of compilation models, but it does create problems with libs

 unaware.. just to be clear: are you saying that this behavior is by

 that no change is anticipated?

Yes. I also don't think you'll find any code otherwise. I think all the existing compilers work that way.
Jan 24 2003
parent reply Richard Grant <fractal clark.net> writes:
In article <b0rq1h$17o7$1 digitaldaemon.com>, Walter says...

Yes. I also don't think you'll find any code otherwise. I think all the
existing compilers work that way.

In some other thread on Template definitions in different translation units, we were talking about irrelevant details. According to 14-7 of the template introduction, The export keyword handles this issue. But this does not work: export template <class T> struct A { // Error: missing decl-specifier-seq for declaration of 'export' void fn(); }; template <class T> void A<T>::fn() { } int main() { A<int> a; a.fn(); } Richard
Feb 28 2003
parent "Walter" <walter digitalmars.com> writes:
Exported templates do not work yet.

"Richard Grant" <fractal clark.net> wrote in message
news:b3ngi1$2jmr$1 digitaldaemon.com...
 In article <b0rq1h$17o7$1 digitaldaemon.com>, Walter says...

Yes. I also don't think you'll find any code otherwise. I think all the
existing compilers work that way.

In some other thread on Template definitions in different translation

 were talking about irrelevant details. According to 14-7 of the template
 introduction, The export keyword handles this issue. But this does not

 export template <class T> struct A {
 // Error: missing decl-specifier-seq for declaration of 'export'
 void fn();
 };

 template <class T> void A<T>::fn() { }

 int main() {
 A<int> a;
 a.fn();
 }

 Richard

Feb 28 2003
prev sibling parent %u Souveek Kumar Bose <imsouveek gmail.com> writes:
For template function declarations and definitions to be in
different files, the declarations must be available in the file in
which they are defined. That is whats the root of all problems in
this case

For template class/function definitions to be in one file and
their declarations in another, you can use one of 2 methods:-

In the file test.h, add #include "test.cpp" after the template
struct definition

Use the export keyword in test.cpp as follows:-
export template<class T> void A<T>::fn() {}
However, most compilers dont support the keyword export. So, you
can do one of two things...
1> Follow method one above
2> Keep template function/struct/class declarations and
definitions in the same file.
Feb 20 2007