c++ 為什麼在返回從函式的返回型別派生的型別的本地物件時,沒有選擇move建構函式?
(中繼版本)拒絕:
#include <memory> struct Base { Base() = default; Base(Base const&) = delete; Base(Base&&) = default; }; struct Derived : Base { Derived() = default; Derived(Derived const&) = delete; Derived(Derived&&) = default; }; auto foo() -> Base { Derived d; return d;// ERROR HERE }
根據[class.copy] / 32:
When the criteria for elision of a copy/move operation are met, but not for an exception-declaration, and the object to be copied is designated by an lvalue,or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression,overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue
如果上面的句子意圖被解析為(複製elision標準符合& lvalue)|| (id-expression指定一個自動物件),正如this CWG defect 所示,為什麼這不是最後一個條件呢?在Clang和GCC中是否有編譯器錯誤?
另一方面,如果這個句子意圖被解析為(copy elision criteria met&(lvalue || id-expression指定一個自動物件)),這不是一個非常誤導性的字面意義嗎?
[class.copy] / 32繼續:
[…] if the type of the first parameter of the selected constructoris not an rvalue reference to the object’s type (possiblycv-qualified), overload resolution is performed again, considering theobject as an lvalue.
第一個過載解析度,以d作為值,選擇Base :: Base(Base&&).然而,所選建構函式的第一個引數的型別不是派生和&但是Base&&&&&&&&&&&&&&&第二個過載解析度選擇刪除的拷貝建構函式.
程式碼日誌版權宣告:
翻譯自:http://stackoverflow.com/questions/40039379/why-is-move-constructor-not-picked-when-returning-a-local-object-of-type-derived