-
September 29th, 2010, 01:47 AM
#1
can I swap the content of an array?
I have a legacy struct containing a char array:
Code:
struct foo
{
int i;
char c[256];
}
I am trying to write a copy constructor and assignment operator using a swap function. My Visual Studio 2003 does not let me
std::swap(foo.c).
Is this a problem of the rather old compiler or is it not possible to std::swap the array content?
-
September 29th, 2010, 01:52 AM
#2
Re: can I swap the content of an array?
All versions of std::swap takes two arguments. But as arrays are not assignable, you cannot use std::swap on them directly. On the other hand, you can already use std::swap on objects of type foo without having to specialise std::swap.
-
September 29th, 2010, 02:36 AM
#3
Re: can I swap the content of an array?
To be more precise, in normal C++, to use std::swap to an object, that objects needs to be both copy constructable and assignable. Your foo struct is a simple POD, so it already has a default constructor, assignment operator, and hence, it can be swapped. You really have nothing to do. Arrays are neither of the above, so cannot be swapped.
That said, the new c++0x swap allows arrays to be swaped with the template specialization of swap<typename T, size_t N>. HOWEVER, the complexity is still N, and swapping an array is just a functional convenience. It does nothing more than swap each array value 1 by 1. Also, C++0x lowers swappable requirements to just "movable".
I also want to add: "I am trying to write a copy constructor and assignment operator using a swap function" - No need to do that, your type has no such need.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 29th, 2010, 06:38 AM
#4
Re: can I swap the content of an array?
I have been trying to use the compiler defined copy constructor, but my copy of an instance of foo did not have the same content as the original in the array named 'c' in my example.
I understand your comments that the content should be copied, so the bug I am hunting must lie elsewhere.
Thanks for your comments,
Richard
-
September 29th, 2010, 09:00 AM
#5
Re: can I swap the content of an array?
Originally Posted by Richard.J
I have been trying to use the compiler defined copy constructor, but my copy of an instance of foo did not have the same content as the original in the array named 'c' in my example.
Code:
#include <cstring>
#include <algorithm>
struct foo
{
int i;
char c[256];
};
int main()
{
foo f1;
f1.i = 100;
strcpy( f1.c, "Joe");
foo f2;
f2.i = 200;
strcpy( f2.c, "Jack");
std::swap(f1, f2);
}
Unless your compiler is broken, this swaps the contents of f1 and f2.
Regards,
Paul McKenzie
-
September 29th, 2010, 12:48 PM
#6
Re: can I swap the content of an array?
Take a look at this just for future reference. If you wanted to swap only the array elements but not the entire object this algorithm can be used.
http://cplusplus.com/reference/algorithm/swap_ranges/
I am in agreement with others though. I'm not clear on what the problem is. There is no need for a copy constructor or assignment operator for that foo struct. The op also wrote swap(foo.c) but I don't see what the .c is for. That looks strange. is that just a typo that is causing the error? Please take Paul's advice and make a small, compilable program. If you still get errors copy and paste the entire program within code tags and with proper formatting and someone will take a look at that.
Last edited by kempofighter; September 29th, 2010 at 12:56 PM.
-
September 30th, 2010, 01:05 AM
#7
Re: can I swap the content of an array?
Thanks for all your responses.
I have been able to spot what I was doing wrong, and it had nothing to do with the array. I was just mislead by my code.
Thanks again,
Richard
-
September 30th, 2010, 03:08 AM
#8
Re: can I swap the content of an array?
There is one more item of interest.
If your structure were to be defined as
Code:
struct foo
{
int i;
vector<char> c;
}
then a swap would likely be a lot faster, as vector's swap function will almost certainly just swap the pointers to the internal dynamic arrays rather than actually copy the data, as would occur in the original definition.
You would have to define a specialised swap function for your structure of course.
If your legacy code only ever uses the array element by index then this may be a useful, and backward compatible, modification.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 30th, 2010, 07:13 AM
#9
Re: can I swap the content of an array?
Originally Posted by JohnW@Wessex
You would have to define a specialised swap function for your structure of course.
Would you? I haven't fully digested move semantics yet, but if the C++0x implementation of std::swap attempts to move rather than copy the object, would the default behavior move rather than copy the vector? Or does a move constructor/assigner not get auto-generated?
-
September 30th, 2010, 07:26 AM
#10
Re: can I swap the content of an array?
quoting the draft in my possession:
The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions. [Note: The implementation will implicitly declare these member functions for some class types when the program does not explicitly declare them. The implementation will implicitly define them if they are used. ...
The implicitly-defned move constructor for a non-union class X performs a memberwise move of its sub-objects ...
-
September 30th, 2010, 07:58 AM
#11
Re: can I swap the content of an array?
Originally Posted by superbonzo
quoting the draft in my possession:
My current MinGW 4.5.0 refuses to explicitly default the move assignment operator, but is fine with the move constructor:
Code:
myClass(myClass&&) = default; //accepted
myClass& operator=(myClass&&) = default; //refused
That said, isn't an assignable object movable by definition? I mean, can you explicitly request that a class implement move?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|