Boost logo

Boost :

Subject: Re: [boost] [interest] underlying type library
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2011-08-20 12:28:16


On 08/20/2011 06:09 PM, Julian Gonggrijp wrote:

> UNDERLYING_TYPE(T) represents a type which is (conceptually speaking)
> a POD-type with the same member data (and hence the same alignment) as
> T.

You mean like boost::aligned_storage<sizeof(T),
boost::alignment_of<T>::value> ?

> The underlying type can be used to allocate space for a T object
> and to temporarily store the state of a T object.
>
> Using move_raw and UNDERLYING_TYPE, a swap algorithm can be
> implemented which works and is efficient for any type:
>
> template<class T>
> void swap (T& x, T& y) {
> UNDERLYING_TYPE(T) temp;
> move_raw (x, temp);
> move_raw (y, x);
> move_raw (temp, y);
> }

This clearly looks like undefined behaviour.

> The benefits of an underlying type library go far beyond the swap
> algorithm. For a start, any algorithm which moves values around
> without an inherent need to copy them could use the same mechanism as
> the swap algorithm above. Apart from that, it can help to simulate
> rvalue references and move semantics in pre-C++11 compilers in an
> efficient way. Concluding, a library which provides these tools has an
> immense potential for improving efficiency and convenience anywhere
> C++ is used.

You cannot use bitwise copy to implement move semantics.
That just doesn't work.


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