Search code examples
c++swapcopy-constructor

How to copy (or swap) objects of a type that contains members that are references or const?


The problem I am trying to address arises with making containers such as an std::vector of objects that contain reference and const data members:

struct Foo;

struct Bar {
  Bar (Foo & foo, int num) : foo_reference(foo), number(num) {}
private:
  Foo & foo_reference;
  const int number;
  // Mutable member data elided
};

struct Baz {
  std::vector<Bar> bar_vector;
};

This won't work as-is because the default assignment operator for class Foo can't be built due to the reference member foo_reference and const member number.

One solution is to change that foo_reference to a pointer and get rid of the const keyword. This however loses the advantages of references over pointers, and that const member really should be const. They are private members, so the only thing that can do harm is my own code, but I have shot myself in the foot (or higher) with my own code.

I've seen solutions to this problem on the web in the form of swap methods that appear to be chock full of undefined behavior based on the wonders of reinterpret_cast and const_cast. It happens that those techniques do appear to work on my computer. Today. With one particular version of one particular compiler. Tomorrow, or with a different compiler? Who knows. I am not going to use a solution that relies on undefined behavior.

Related answers on stackoverflow:

So is there a way to write a swap method / copy constructor for such a class that does not invoke undefined behavior, or am I just screwed?

Edit
Just to make it clear, I already am quite aware of this solution:

struct Bar {
  Bar (Foo & foo, int num) : foo_ptr(&foo), number(num) {}
private:
  Foo * foo_ptr;
  int number;
  // Mutable member data elided
};

This explicitly eliminates the constness of number and the eliminates the implied constness of foo_reference. This is not the solution I am after. If this is the only non-UB solution, so be it. I am also quite aware of this solution:

void swap (Bar & first, Bar & second) {
    char temp[sizeof(Bar)];
    std::memcpy (temp, &first, sizeof(Bar));
    std::memcpy (&first, &second, sizeof(Bar));
    std::memcpy (&second, temp, sizeof(Bar));
}

and then writing the assignment operator using copy-and-swap. This gets around the reference and const problems, but is it UB? (At least it doesn't use reinterpret_cast and const_cast.) Some of the elided mutable data are objects that contain std::vectors, so I don't know if a shallow copy like this will work here.


Solution

  • If you implement this with move operators there is a way:

    Bar & Bar :: operator = (Bar && source) {
        this -> ~ Bar ();
        new (this) Bar (std :: move (source));
        return *this;
    }
    

    You shouldn't really use this trick with copy constructors because they can often throw and then this isn't safe. Move constructors should never ever throw, so this should be OK.

    std::vector and other containers now exploit move operations wherever possible, so resize and sort and so on will be OK.

    This approach will let you keep const and reference members but you still can't copy the object. To do that, you would have to use non-const and pointer members.

    And by the way, you should never use memcpy like that for non-POD types.

    Edit

    A response to the Undefined Behaviour complaint.

    The problem case seems to be

    struct X {
        const int & member;
        X & operator = (X &&) { ... as above ... }
        ...
    };
    
    X x;
    const int & foo = x.member;
    X = std :: move (some_other_X);
    // foo is no longer valid
    

    True it is undefined behaviour if you continue to use foo. To me this is the same as

    X * x = new X ();
    const int & foo = x.member;
    delete x;
    

    in which it is quite clear that using foo is invalid.

    Perhaps a naive read of the X::operator=(X&&) would lead you to think that perhaps foo is still valid after a move, a bit like this

    const int & (X::*ptr) = &X::member;
    X x;
    // x.*ptr is x.member
    X = std :: move (some_other_X);
    // x.*ptr is STILL x.member
    

    The member pointer ptr survives the move of x but foo does not.