Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-12-11 09:14:32


"Fernando Cacciola" <fernando_cacciola_at_[hidden]> 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())

> 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?

-- 
                       David Abrahams
   dave_at_[hidden] * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk