I was just thinking if the new C++11 in-class member initializers could be used to initialize Singletons at compile-time, which might be a speed-up for some Manager-Classes in my applications:
class CSingleton
{
public:
CSingleton(void) {}
~CSingleton(void) {}
static const CSingleton* GetInstance(void)
{
return Instance;
}
bool Foo1(int x);
bool Foo2(int y);
private:
static constexpr CSingleton *Instance = new CSingleton();
}
The problem is this results in following errors:
Line of Instance declaration: error: invalid use of incomplete type 'class test::CSingleton'
First Line of class declaration: error: forward declaration of 'class test::CSingleton'
Is there a way to initialize Singletons during compile-time with this or another approach?
[I am using GCC4.7 on MacOSX10.7 (and Ubuntu) with -std=c++0x flag set]
in .h file member of class:
in .cpp file in the begining right after include
This is initialization in compile time.
using new – this is initialization in runtime. Formally both of them initialization in compile time.