----- Original Message ----- From: "David Abrahams" <[EMAIL PROTECTED]> To: "Boost mailing list" <[EMAIL PROTECTED]> Sent: Wednesday, December 11, 2002 11:14 AM Subject: Re: [boost] Formal review: Optional library
> "Fernando Cacciola" <[EMAIL PROTECTED]> writes: > > > Currently, the non-POD case implementation goes like this: > > > > (this is a sketch actually) > > > > template<class T> > > class optional1 > > { > > optional1 ( T const& v ) { p = new (buffer.address()) T(v) ; } > > > > T const& operator *() { return *p; } // Dereference here > > > > aligned_storage<T> buffer ; > > T* p ; > > } ; > > There's no need to store p. You can just use > static_cast<T*>(buffer.address()) > Yes.. I just shown the pointer explicitely. The result of the static_cast<> is a pointer, so to obtain a reference (for the operator*) you need to 'actually' dereference this poiner. > > OTOH, when T is a POD, so there is no need to bypass its ctor > > (since it has a trivial ctor), > > the implementation goes like this: > > > > template<class T> > > class optional2 > > { > > optional2 ( T const& val ) v(val) {} > > > > T const& operator *() { return val; } // No Dereference here > > > > T val ; > > } ; > > > > Anyway, if yoy know how to make an efficient implementation like > > the second one with an aligned storage I'll be happy to use it. > > Why do you think that will be any more efficient than the other > implementation when T is POD? > For POD types: operator*() "directly" returns the value. The key is that for POD types, since optional wraps just T (unlike any variant) and nothing else, aligned_storage is not required. optional<> simply contains a data member of type T, that's all. If you still don't see it I can show you the typical machine generated code for both implementations for operator*(). Fernando Cacciola _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost