It is not a very neat idea. but, I intend to write a class with a static member being a derived instance of itself. (actually, my class is a kind of abstract container and I am sharing a concrete instance of a simple empty container for performance reasons).
I checked this code and it compiles OK :
class B;
class A {
public :
static B b;
};
class B : public A
{};
int main()
{
A a;
}
But my class is actually a template one :
template<typename T>
class B;
template<typename T>
class A {
public :
static B<T> b;
};
template<typename T>
class B : public A<T>
{};
int main()
{
A<int> a;
}
Unfortnuately, I now have a compiler error :
main_ko2.cpp: In instantiation of ‘class B<int>’:
main_ko2.cpp:7:15: required from ‘class A<int>’
main_ko2.cpp:14:9: required from here
main_ko2.cpp:10:7: error: invalid use of incomplete type ‘class A<int>’
10 | class B : public A<T>
| ^
main_ko2.cpp:5:7: note: declaration of ‘class A<int>’
5 | class A {
| ^
Note that if I add the following line in my main()
function
int main()
{
B<int> b; //!< added
A<int> a;
}
Now, the code compiles OK !
As it is an uncommon (probably for good reason) programming pattern, maybe I stand in the dark areas where compiler behavior is unspecified. Maybe other compilers would behave differently than mine : g++ (Ubuntu 11.3.0-1ubuntu1~22.04 11.3.0)
What do you think ? Would there be a trick to make the compilation works ?
I don't think creating global variables would be a good solution as I want an instance for any types in my template argument.