Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-10-08 22:48:28

--- David Abrahams <dave_at_[hidden]> wrote:

> Please elaborate.

Here is how I would do it.

Below is a working psedo-code. I tested it and it worked ok
including assignment exceptions.
The sample is a simple type holder that provides
basic guarantees during assignments.

template< typename T >
struct largest_size
      value = sizeof(T)

//doesn't throw
void variant_copy( char* dst, char* src, size_t size )
    for( size_t i = 0; i < size; ++i, ++dst, ++src )
        *dst = *src;

template< typename T >
struct stack_variant
    char data_[largest_size<T>::value];
    stack_variant() { new(data_) T; }
    stack_variant<T>& operator=( const T& l )
        const int size = largest_size<T>::value;
        char b1[size];
        char b2[size];
        //save the current data
        variant_copy( b1, data_, size );
            //place new object in _data
            T* obj = new(data_) T;
            //copy data
            *obj = l;
            //copy the new object to another storage
            variant_copy( b2, data_, size );
            //copy the original object back for destruction
            variant_copy( data_, b1, size );
            //put the new object back in place
            variant_copy( data_, b2, size );
        catch( ... )
            variant_copy( data_, b1, size );
        return *this;


Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

Boost list run by bdawes at, gregod at, cpdaniel at, john at