Topic: instantiation of static data members of class templates
Author: Martin Sebor <sebor@roguewave.com>
Date: Tue, 12 Jun 2001 21:13:17 GMT Raw View
Poul Thomas Lomholt wrote:
>
> Hello, I am trying to come up with an implementation of the abstract factory
> pattern, and my idea is to introduce a class template as a baseclass, which
> then will instantiate a static data member to hold various creator
> information.
> However, I am getting a problem where the compiler doesn't instantiate this
> static member, and I have difficulties understanding why.
Because of the rules of template instantiation.
14.7.1 says that implicit template instantiation causes the implicit
instantiation of the declarations, but not of the definitions of static
data members.
Regards
Martin
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Poul Thomas Lomholt" <ptlomholt@hotmail.com>
Date: Fri, 8 Jun 2001 17:23:07 GMT Raw View
Hello, I am trying to come up with an implementation of the abstract factory
pattern, and my idea is to introduce a class template as a baseclass, which
then will instantiate a static data member to hold various creator
information.
However, I am getting a problem where the compiler doesn't instantiate this
static member, and I have difficulties understanding why.
To boil it down, I created the following simplified example:
#include <stdio.h>
struct B
{
B() { printf("B::B()"); }
};
template <class T>
struct A // this would be used as a baseclass for classes that wanted to be
factory-enabled, ie: "class MyClass : public A<MyClass> ..."
{
static B instance; // this instance would hold information on how to
create an object of type T, its constructor would add itself to a linked
list
};
template <class T>
B A<T>::instance;
class MyClass : public A<MyClass>
{
};
int main()
{
// nothing should be needed here to make it work...
return 0;
}
I would expect one instance of B being created, thus printing "B::B()". It
doesn't work with either VC6SP5, Borlands 5.5 or gcc on Windows. The code
compiles fine but B::B() never gets called
The question is, am I missing something here, it seems weird that all
compilers would be wrong. I appreciate any input you might have to clarify
this mystery.
Thanks
/Poul Thomas Lomholt
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]