469,945 Members | 2,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

composite types and std::swap

Consider the source snippet.
# include <iostream>
struct foo_struct {
int odx ;
int pdx ;
foo_struct ()
: odx ( 0 )
, pdx ( 0 )
{}
};

int main()
{

foo_struct fs1;
fs1.odx = 2 ;
fs1.pdx = 5 ;
foo_struct fs2;
std::swap ( fs1, fs2 ) ;
}

Ideally I could provide my own copy constructor and assignment
operator for foo_struct, nonetheless, I'm more interested in whether
the use of std::swap on the composite ( non-POD types ) makes the
program ill-formed?

Thanks in advance.

Mar 24 '07 #1
1 1866
* ma740988:
>
# include <iostream>
struct foo_struct {
int odx ;
int pdx ;
foo_struct ()
: odx ( 0 )
, pdx ( 0 )
{}
};

int main()
{

foo_struct fs1;
fs1.odx = 2 ;
fs1.pdx = 5 ;
foo_struct fs2;
std::swap ( fs1, fs2 ) ;
}

Ideally I could provide my own copy constructor and assignment
operator for foo_struct, nonetheless, I'm more interested in whether
the use of std::swap on the composite ( non-POD types ) makes the
program ill-formed?
No.

The effect is the same as

foo_struct temp;
temp = fs1; fs1 = fs2; fs2 = temp;

which just uses the compiler-generated copy assignment operator.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 24 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Davis King | last post: by
2 posts views Thread by ma740988 | last post: by
1 post views Thread by Sung Jin Hwang | last post: by
4 posts views Thread by Niels Dekker (no reply address) | last post: by
9 posts views Thread by ma740988 | last post: by
8 posts views Thread by ma740988 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.