cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

The confusing way moves are broken

Open lefticus opened this issue 1 year ago • 1 comments

struct S {
  ~S() {}
};

The above type does not have a move constructor or move assignment, but it still works with move operations and is_move_constructible returns true.

So how do we show that move operations don't exist?

https://compiler-explorer.com/z/dva81543s

#include <utility>
#include <type_traits>

struct Contained {
  Contained();
  Contained(const Contained &);
  Contained(Contained &&);
  Contained &operator=(const Contained &);
  Contained &operator=(Contained &&);
  ~Contained();
};

struct S {
  Contained c;
  // comment this out and see the difference
  ~S();
};

S getS();

static_assert(std::is_move_constructible_v<S>);
static_assert(std::is_move_assignable_v<S>);

void useS(S);

int main()
{
  S obj;
  obj = getS();
  useS(std::move(obj));
}

lefticus avatar Jul 12 '24 20:07 lefticus