Boost logo

Boost Users :

From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-05-30 09:24:47


On Fri, May 30, 2008 at 7:17 AM, Roman Perepelitsa
<roman.perepelitsa_at_[hidden]> wrote:
> Scott McMurray <me22.ca+boost <at> gmail.com> writes:
>> std::copy( binary_buffer.begin(), binary_buffer.end(),
>> reinterpret_cast<char*>(&dr) );
>>
>
> Or:
> memcpy(&dr, &buffer[0], buffer.size());
>

Though a good implementation will have implemented std::copy for bytes
with memcpy.

> Or use union to avoid making a copy:
>
> union {
> data d;
> unsigned char bytes[4];
> } u;
>
> bytes[0] = 3;
> bytes[1] = 6;
> bytes[2] = 0;
> bytes[3] = 9;
>
> // use d.first, d.second, d.third
>

If your compiler allows that as an extension. (GCC does, dunno about others.)

It's officially not allowed, last I checked.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net