lundi 24 août 2015

Automatically generated move constructor with not movable members

I got in a situation which is quite interesting as the code I'm working on compiles even though I'm surprised it does so I would like to ask you for your take.

The situation is this: I have a class with user defined assignment for l- and r-value references but deleted move and copy constructors, let's call it A. I won't go in the reason for this choice which I realize is a bit unusual but that's why I never came into this issue before.

Now we have another class which contains a member of the first class, let's call it B. In this class I defined the copy constructor but I kept the move constructor as default and defined the assignment operator through a call to the swap function:

class B{
public:
A a;

B():
    a{}
{
    // Some initialization
}

B(const B& other):
    a{}
{
    // Some initialization
}

B&
operator=(B other)
{
    std::swap(*this, other);
    return *this;
}

B(B&& other) = default;

As far as I knew the swap operation is equivalent to a sequence of moves between the two involved parties and a temporary and, in fact, if I explicitly delete the move constructor the compiler comp(i)lains.

So far so good but what I don't understand is: why the default move constructor works, considering that it cannot simply call the move or copy constructor A, which I thought was the typical implementation in those cases?

I'm using gcc 4.8.4 at the moment

Aucun commentaire:

Enregistrer un commentaire