I'm trying to understand why the compiler failed with an error code C2280 (attempting to reference a deleted function) when the deleted function is never used.
Here an example of code that fails
class A {
public:
A() {
cout << "default" << endl;
}
A(A const&) {
cout << "copy" << endl;
}
A(A&&) = delete;
A create() {
return A();
}
};
void main() {
A a;
A b(a.create());
}
And the same code that I don't understand
class A {
public:
A() {
cout << "default" << endl;
}
A(A const&) {
cout << "copy" << endl;
}
A(A&&) {
cout << "move" << endl;
}
A create() {
return A();
}
};
void main() {
A a;
A b(a.create());
}
This code compiles but doesn't display "move" on the console.
The result is
default
default
Before C++17 copy elision was an optional optimization, but compiler still have to check that copy/move constructor available. Starting from C++17 your code will compile without error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.