473,387 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

swap() implementation curiosity

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);
}
};

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #1
1 1662
"bartek" <sp******************@o2.pl> wrote in message
news:Xn**********************************@153.19.2 51.200...
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?


It's just plain completely wrong.

Consider, for example, a class for which every constructor enters the
address of the object being constructed into a global table, and for which
the destructor removes the object's address from that table. This
implementation of swap would completely screw up such a data structure.
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Davis King | last post by:
Can I assume that std::swap will not throw when it is used to swap std::string objects? In the standard it says that the swap() std::string member function runs in constant time. I didn't see...
4
by: kalita | last post by:
Hi All, typedef std::list<int> Cont; void f(Cont &c1, Cont &c2) { Cont::iterator it = c1.begin(); c1.swap(c2); it == c2.begin(); // is this ill formed? }
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
19
by: Krishanu Debnath | last post by:
Hello, I have a call to hash_map::clear() function which takes long time. someClass::someFunction() { // typedef hash_map<name_id, uintMp; // Mp p; // assuming proper namespace, hash...
22
by: Pramod | last post by:
Hello to all of you, basically i want a program how to swap value...i know 2 method 1st --- temp=a; a=b; b=temp; 2nd---- a=a+b; b=a-b; a=a-b;
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
25
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce...
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.