"Shen" <sy*******@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
I saw this conclusion in a couple of groups. For example, if you
define a class template, in VC++ you have to merge the .h and .cpp
files into one file and include it in other files where they have
something to do with this template class. Otherwise you'll get Link
errors(unresolved external symbol...LNK2001/LNK2019).
Is it still true now? Has MS Visual studio 2003 done something for
it? or does anyone figure out a solution other than changing the .h/.cpp
style?
It is not good to include implementation in other files, for it
tends to put redefinition bugs into your project and...anyway it is off
the .h/.cpp way.
There is a language feature called export which allows you include
function template and class template static data definitions in .cpp
files. Last I header, microsoft had no intention ever to implement
this feature.
I have never used export, because of its limited availability, but I
think you can do something like this:
------------------------------
"my_template.hpp":
------------------------------
#ifdef HAS_EXPORT
#define EXPORT export
#else
#define EXPORT
#endif
EXPORT
template<typename T>
struct my_template {
void f();
static int* g;
};
#ifndef HAS_EXPORT
#include "my_template.cpp"
#endif
------------------------------
"my_template.cpp"
------------------------------
#ifndef HAS_EXPORT
#include "my_template.h"
#endif
template<typename T>
void my_template<T>::f() { /* */ }
template<typename T>
int* my_template<T>::g;
-------------------------------------
This allows you to take advanage of export where it is available.
(Conceivably there could be subtle difference in the meanings of
programs depending on whether HAS_EXPORT is defined, because of
name-lookup rules.)
Jonathan