CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: can I swap the content of an array?

    Quote Originally Posted by Richard.J View Post
    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

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb 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.

  7. #7
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: can I swap the content of an array?

    Quote Originally Posted by JohnW@Wessex View Post
    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?

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    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 ...

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: can I swap the content of an array?

    Quote Originally Posted by superbonzo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured