Why is move constructor not picked when returning a local object of type derived from the function's return type?

后端 未结 1 513
情歌与酒
情歌与酒 2020-12-18 20:01

The following code is rejected by both Clang and GCC (trunk versions):

#include 

struct Base 
{
    Base() = default; 
    Base(Base const&         


        
相关标签:
1条回答
  • 2020-12-18 20:42

    [class.copy]/32 continues:

    [...] if the type of the first parameter of the selected constructor is not an rvalue reference to the object's type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue.

    The first overload resolution, treating d as an rvalue, selects Base::Base(Base&&). The type of the first parameter of the selected constructor is, however, not Derived&& but Base&&, so the result of that overload resolution is discarded and you perform overload resolution again, treating d as an lvalue. That second overload resolution selects the deleted copy constructor.

    0 讨论(0)
提交回复
热议问题