Syntax initialization at compile time

I just wondered if the new C ++ 11 class member initializers in the class could be used to initialize the Singletons at compile time, which could be an acceleration 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:

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 at compile time with this or another approach?

[I use GCC4.7 on MacOSX10.7 (and Ubuntu) with the -std = C ++ 0x flag set]

+3
source share
1 answer

in .h member of the class file:

static CSingleton s_Instance;

in the .cpp file at the beginning immediately after inclusion

CSingleton::s_Instance = CSingleton();

This is initialization at compile time. using new is runtime initialization. Formally, both of them are initialized at compile time.

0
source

All Articles