
Things like auto_ptr aside, you'd need to templatize the once_flag on the return type then, since the first time value needs to be stored somewhere. Is this worth the hassle?
I think yes.
How about the attached version? The result is stored on the heap, which adds some performance cost, but the once_flag itself doesn't need templating. I haven't yet adjusted it to avoid this heap allocation for void functions.
It does mean that the initializer is now an aggregate initializer rather than a constant, as for my original Semaphore-based version.
Interesting, but don't you end up with the classic singleton problem of "when do I delete this object?", doesn't this implementation risk deleting the object before the last call to the function? There may be workarounds, but I'm not convinced at the moment that they are worth the hassle, are we convinced that having a return value is really that important? I also have a slightly reworked version of call_once for you to consider, it's basically the same as your last version, but does away with the need to use stringstreams when creating the mutex name - this cuts down on the memory allocation calls, which should be a big win the first time through. Regards, John.