I want to realize a singleton-like functionality using std::call_once just for fun or perhaps could improve on the Singleton Pattern itself. Here's what I tried so far but I am stuck. Any help will be highly appreciated.
class single {
public:
private:
single(){}
friend void boo(unique_ptr<single>&);
};
void boo(unique_ptr<single>& f) {
f.reset(new single());
}
unique_ptr<single>& bar() {
static once_flag flag;
static unique_ptr<single> f;
call_once(flag, boo,f);
return f;
}
int main()
{
unique_ptr<single> f;
f = move(bar());
unique_ptr<single> f2;
f2 = move(bar()); // this should not work but it does, work-around?
}