I have the following code.
#include <iostream>
struct Box {
Box() { std::cout << "constructed at " << this << '\n'; }
Box(Box const&) { puts("copy"); }
Box(Box &&) = delete;
~Box() { std::cout << "destructed at " << this << '\n'; }
};
auto f() {
Box v;
return v; // is it eligible for NVRO?
}
int main() {
auto v = f();
}
The above code produces error. call to deleted constructor/function in both gcc and clang
But If I change the code to return prvalue, the code works.
auto f() {
Box v;
return Box(); // is it because of copy elision?
}
Why is this happening? Is it because of delete move constructor? If I change both copy and move constructors to explicit, it also produces error?
If marked deleted, why can't it simply use the copy constructor as it is defined
Edit:
compiled with -std=c++20 in both gcc and clang, error.
compiled with -std=c++17 gcc, compiles.
compiled with -std=c++17 clang, error.
Edit 2:
clang version: 12.0.0
gcc version: 11.1