I want to use boost::call_once() to achieve a thread-safe lazy-construction
singleton scenario, however, the base singleton class has many derived classes thus the getInstance() function takes an argument to determine which derived class to initialize. The code looks like,
Singleton * Singleton::getInstance(Input * a) {
if (!instance) {
instance = buildme(a); //buildme() will return a derived class type based on input a.
}
return instance;
}
I want to use boost::call_once(), but looks like it can only be used on functions with no arguments void (*func)(). If anybody knows about an alternative solution here please help.
Thanks.
EDIT::
Another question, how to call a non-static member function using call_once? I have a non-static init() member function of this class, but I couldn’t find a correct syntax for calling it using boost::call_once(). Or should I make init() and everything used in it static?
Thanks.
You can bind additional function parameters to a functor object using
boost::bind. Like this:You can use
boost::bindto call non-static member functions as well, by passing the instance of the class on which you want to call the function toboost::bind.With C++11, you can use
std::bind, here’s another example.boost::bindis quite similar.