I\'ve got a singleton that is expensive to initialize:
struct X {...};
const X&
get_X()
{
static const X x = init_X();
return x;
}
If you don't mind the delay when your application starts, you could make x
as a static private member of the class, like this
#include
class X {
public:
static X const& get_x();
private:
static X const x;
X()
{
std::cout << "X init" << std::endl;
}
};
int main()
{
std::cout << "Enter main" << std::endl;
X::get_x();
return 0;
}
X const X::x;
X const& X::get_x()
{
return X::x;
}