"Christof Warlich" <cw******@gmx.dewrote in message
news:44**********************@newsspool1.arcor-online.net
John Carson wrote:
>In that case you have simplified the problem to the point of removing
essential features. I take it that doSomething and X are both
templates. Then what is wrong with:
template<typename Tclass Z {};
template<typename Tclass X {};
template<typename T>
int doSomething(void)
{
Z< X<T z;
return 0;
}
You are right, I've probably stripped down things too much. But some
additional explanations may do:
My intention was to separate template declaration and definition (See
FAQ Lite: How can I avoid linker errors with my template functions).
Doing this, I wanted to minimize the lines of code that have to be
written to explicitly instantiate my templates (in this simple case,
just instantiating doSomething() for the required types instead of
having to instantiate doSomething() and class X).
You could turn your function into a functor, like std::cout. To get this to
work of course, you need to declare an object of the class, either (i) in
client code or (ii) in the implementation file, with an extern declaration
in the header. This gives an extra line (or two), so you may not think this
approach offers an advantage --- though it still could if you had classes A,
B, C... as well as X.
First, a version where it all goes in the header:
template<typename T>
class Z {};
template<typename T>
class doSomething
{
class X {};
public:
int operator()()
{
Z<Xz;
return 0;
}
};
// need to also declare an object of the class
Splitting this into .h and .cpp files, we would have:
////// foo.h file ///////////////////////////
template<typename T>
class doSomething
{
class X;
public:
int operator()();
};
extern doSomething<intds;
/////// foo.cpp file //////////////////////
#include "foo.h"
template<typename T>
class Z {};
template<typename T>
class doSomething<T>::X
{};
template<typename T>
int doSomething<T>::operator()()
{
Z<Xz;
return 0;
}
// only one instantiation line required
template class doSomething<int>;
// but need this line unless client code has it
doSomething<intds;
//////// main.cpp file /////////////////////
#include "foo.h"
int main()
{
ds();
}
/////////// end files ///////////////////////
The above seems to work with VC++ 2005.
--
John Carson