Why does the following code work, although I explicitly deleted the move constructor?
class X { public: X() = default; X(const X&) = delete; X(X&&) = delete; int val = 42; }; template<typename T> int foo(T x) { return x.val; } int main() { X x1; //X x2{x1}; // error //X x3{std::move(x1)}; // error //foo(x1); // error //foo(std::move(x1)); //error foo(X{}); // OK? } What restrictions are imposed on type T besides that it must have a member val that is cast to an int ? Is it possible to ban the last transfer?
foo(X{});move the constructor is not called. The last pass can be disabled by disabling the default constructor. Well, or you can use the older standards of the language. - VTT