|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2003-10-09 08:50:28
Brian McNamara <lorgon_at_[hidden]> writes:
> 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.
Yes, but not for anything else. This is exactly the memcpy trick that
Eric was referring to earlier. The fact that you spelled it
"variant_copy" and not "memcpy" doesn't change anything; it has the
same semantics. I don't know why we have to keep going over the same
ground over and over again. All of this is in the discussion
archives. If people would just go back and do some review we could
avoid lots of wasted bandwidth and, I dare say, aggravation.
> 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?
No, you can't.
When people reviewed all the variant design discussions which led us
here, and I'll tune back in. For now, I'm tuning out.
Regards,
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk