Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-10-08 23:17:35


On Wed, Oct 08, 2003 at 08:48:28PM -0700, E. Gladyshev wrote:
> Here is how I would do it.
...
> //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;
> }
> }
...
> //save the current data
> variant_copy( b1, data_, size );
...
> //copy the new object to another storage
> variant_copy( b2, data_, size );
> //copy the original object back for destruction
> variant_copy( data_, b1, size );
> static_cast<T*>((void*)data_)->~T();
> //put the new object back in place
> variant_copy( data_, b2, size );

This is clever. Is it legal? I know it's legal for POD types. I can't
find text in the standard which makes it legal for non-POD types. But
at the same time, offhand I can't imagine an implementation where it
wouldn't work.

Can you treat a non-POD type as a "bag of bits" temporarily, just to
move it off to the side, and then move it back to the same location and
treat it as an object again?

-- 
-Brian McNamara (lorgon_at_[hidden])

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