Tech Talk about C++ Templates / Comeau C++ Template FAQ: "What's the export keyword about?
Why do I get a link error when compiling my templates?
Although Standard C++ has no such requirement, some compilers require that all function templates need to be made available in every translation unit that it is used in. In effect, for those compilers, the bodies of template functions must be made available in a header file. To repeat: that means those compilers won't allow them to be defined in non-header files such as .cpp files. To clarify, in C++ese this means that this:
// ORIGINAL version of xyz.h
template
struct xyz
{
xyz();
~xyz();
};
would NOT be satisfied with these definitions of the ctor and dtors:
// ORIGINAL version of xyz.cpp
#include 'xyz.h'
template
xyz::xyz() {}
template
xyz::~xyz() {}
because using it:
// main.cpp
#include 'xyz.h'
int main()
{
xyz xyzint;
return 0;
}
will produce an error. For instance, with Comeau C++ you'd get:
C:\export>como xyz.cpp main.cpp
C++'ing xyz.cpp...
Comeau C/C++ 4.3.4.1 (May 29 2004 23:08:11) for MS_WINDOWS_x86
Copyright 1988-2004 Comeau Computing. All rights reserved.
MODE:non-stric"
No comments:
Post a Comment