0
votes

I was looking up default move constructors and read that some classes have deleted default move constructors. http://en.cppreference.com/w/cpp/language/move_constructor

Deleted implicitly-declared move constructor

The implicitly-declared or defaulted move constructor for class T is defined as deleted in any of the following is true:

  • T has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors);

  • T has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors);

  • T has direct or virtual base class with a deleted or inaccessible destructor;

  • T is a union and has a variant member with non-trivial copy constructor;

So when it says data members that cannot be moved, does it also include pointer/reference to the non-movable classes?

1
Nope. "Cannot be moved" has a very precise definition (in parentheses right next to the phrase).n. 1.8e9-where's-my-share m.

1 Answers

0
votes

Both pointers and references are both copyable and movable.
Whether the underlying type is or not, makes no difference.

Consider:

#include <iostream>
#include <utility>

class no_move_or_copy
{
public:
    no_move_or_copy(int i) : i(i) {}
    no_move_or_copy(const no_move_or_copy&) = delete;
    no_move_or_copy(no_move_or_copy&&) = delete;
    operator int() const { return i; }
private:
    int i;
};

class X
{
public:
    X(no_move_or_copy& i, no_move_or_copy& j) : i(i), j(&j) {}
    X(const X&) = default;
    X(X&&) = default;
    operator int() const { return i+*j; }
private:
    no_move_or_copy& i;
    no_move_or_copy* j;
};

int main()
{
    no_move_or_copy i{7}, j{8};
    X x(i,j);
    X y(x);
    X z( std::move(y) );

    std::cout << x << y << z << " done" << std::endl;
}

This will compile, and produce the output

151515 done

Neither pointers nor references changes from being moved from.

See online example