B
bartek
Hi,
Consider the following implementation of swap() for some class...
It looks a little bit like a hells-spawn.
Though it should work for every object with continuous memory layout,
wouldn't it? The class also wouldn't need to be a POD, because such
swapping shouldn't make any difference for it, even if it had a non-
trivial copy/assignment...
.... or is it just plain completely wrong?
typedef unsigned char Byte;
struct Something {
// ...
void Swap(Something& other)
{
if (this == &other) return;
// Buffer to hold contents of the object.
Byte buffer[sizeof(Something)];
Byte *src, *dst;
src = reinterpret_cast<Byte*>(this);
dst = buffer;
std::copy(src, src+sizeof(Something), dst);
src = reinterpret_cast<Byte*>(&other);
dst = reinterpret_cast<Byte*>(this);
std::copy(src, src+sizeof(Something), dst);
src = buffer;
dst = reinterpret_cast<Byte*>(this);
std::copy(src, src+sizeof(Something), dst);
}
};
Consider the following implementation of swap() for some class...
It looks a little bit like a hells-spawn.
Though it should work for every object with continuous memory layout,
wouldn't it? The class also wouldn't need to be a POD, because such
swapping shouldn't make any difference for it, even if it had a non-
trivial copy/assignment...
.... or is it just plain completely wrong?
typedef unsigned char Byte;
struct Something {
// ...
void Swap(Something& other)
{
if (this == &other) return;
// Buffer to hold contents of the object.
Byte buffer[sizeof(Something)];
Byte *src, *dst;
src = reinterpret_cast<Byte*>(this);
dst = buffer;
std::copy(src, src+sizeof(Something), dst);
src = reinterpret_cast<Byte*>(&other);
dst = reinterpret_cast<Byte*>(this);
std::copy(src, src+sizeof(Something), dst);
src = buffer;
dst = reinterpret_cast<Byte*>(this);
std::copy(src, src+sizeof(Something), dst);
}
};