
From: Jason Hise <chaos@ezequal.com>
Rob Stewart wrote:
As I understand it, the operation is inherently unsafe; it isn't a matter of efficiency, or even desirable usage. Given that, it is most appropriate that you spell things differently. You want to call attention to such usage.
Even though the usage is undesirable, it may still be necessary in cases where the singleton needs to behave polymorphically like one of its base classes.
But you said that the object may not exist since it is operator ->() that does the lifetime management. Thus, the object may not actually exist when you call the unsafe function, so it is a dangerous operation. Did I misunderstand something?
Having said that, is it really the case that operator *() couldn't do the same work as operator ->() (possibly adding an exception if you can't get the underlying object)?
The difference is that the pointer returned by operator -> is guaranteed to be used immediately, and by using an intermediate pointer class I can construct a lock for the duration of the member call (this technique is described in Modern C++ Design, in the smart pointer section).
So you're returning a proxy from operator ->(), right?
Operator * is incapable of constructing this type of temporary lock, and additionally there is no guarantee that the reference returned would used and discarded immediately. Once the actual reference is returned, no more checking can take place.
Why can't you return such a proxy from a member function like "get" or "get_object" and let that proxy convert to the singleton type? That means you can spell "get_unsafe_ptr" without "unsafe" because the proxy ensures that it is safe (as safe as with operator ->(), anyway). -- Rob Stewart stewart@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;