正如預期的那樣以下不會編譯:移動構造繞過拷貝構造函數
class A {
public:
A() = default;
//A(A&&) = default;
A(const A&) = delete;
int x;
};
int main()
{
auto a4 = A{}; // not ok, copy constructor is deleted
auto a5 = A(); // not ok, copy constructor is deleted
return 0;
}
但如果添加一個移動的構造函數,即使拷貝構造函數顯式刪除,那麼下面就編譯:
class A {
public:
A() = default;
A(A&&) = default;
A(const A&) = delete;
int x;
};
int main()
{
auto a4 = A{}; // now ok, even though copy constructor is deleted
auto a5 = A(); // now ok, even though copy constructor is deleted
return 0;
}
爲什麼不考慮刪除的拷貝構造函數?
因爲「A {}」或「A()」產生可變的非常量純正確值。 – Lingxi
'auto a6 = a5;'會出錯。 – Jarod42