L
Leroy van Engelen
Hi group,
Say, I wanted to create a class like the following:
template < typename T >
struct Foo {
static T *bar;
};
template < typename T > T *Foo< T >::bar = 0;
And then use it like this:
int n;
Foo< int >::bar = &n;
This will work ok if done within a single .cpp file. But to be really
useful such a class should be put in a header file. However, this will
not work: because the class is a template, its implementation should be
completely put inside the header, but the static variable, bar, needs to
be declared inside an object file. This is not possible however, because
the type (and thus the storage size) of this variable depends on the
template argument and thus cannot be known in advance.
It is possible to put the following in _one_ (no more, no less!!!) of
the .cpp files to circumvent the problem:
template <> int *Foo< int >::bar = 0;
This is rather ugly: the client of the code is now exposed to
implementation details. Not good!
My question: is there nice, clean answer to this problem?
I hope I've explained it good enough
Thanks,
-Leroy
Say, I wanted to create a class like the following:
template < typename T >
struct Foo {
static T *bar;
};
template < typename T > T *Foo< T >::bar = 0;
And then use it like this:
int n;
Foo< int >::bar = &n;
This will work ok if done within a single .cpp file. But to be really
useful such a class should be put in a header file. However, this will
not work: because the class is a template, its implementation should be
completely put inside the header, but the static variable, bar, needs to
be declared inside an object file. This is not possible however, because
the type (and thus the storage size) of this variable depends on the
template argument and thus cannot be known in advance.
It is possible to put the following in _one_ (no more, no less!!!) of
the .cpp files to circumvent the problem:
template <> int *Foo< int >::bar = 0;
This is rather ugly: the client of the code is now exposed to
implementation details. Not good!
My question: is there nice, clean answer to this problem?
I hope I've explained it good enough
Thanks,
-Leroy