The source code of singleton of boost is there ,I don’t understand two notations in the source file below:
// ***include this to provoke instantiation at pre-execution time***
static void use(T const &) {};
BOOST_DLLEXPORT static T & get_instance() {
static detail::singleton_wrapper< T > t;
***// refer to instance, causing it to be instantiated (and
// initialized at startup on working compilers)***
BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);
use(instance);
return static_cast<T &>(t);
}
Question is: How could this code force initialization of singleton in c++ before main()?What do these two notation mean?
It can’t. It’s this line that does:
It creates a static object that is initialized by a call to
get_instance. Since it’s a class-static object, it’s initialized before main.