A friend of mine stumbled on a question
Is there any way to return a copy of an object which is copy-able but NOT move-able. In other words, can we make the following code work?
struct A {
A() = default;
A(A const&) = default; // copyable
A& operator=(A const&) = default; // assignable
A(A &&) = delete; // not movable
A& operator=(A &&) = delete; // not movable
~A() = default;
};
A foo() {
A a;
return a;
}
int main() {
A a(foo()); //will fail, because it calls A(A&&)
return 0;
}
In my opinion, we can't, because foo()
is an A&&
an then the compiler has to call A(A&&)
. But I would like some kind of confirmation.