c++ 涉及move构造函数和’Derived(Base \u0026\u0026)’构造函数的gcc和clang之间的重载分辨率差异

GCC(用4.9测试)接受以下测试用例:

struct Base {};

struct Derived : Base {
    Derived();
    explicit Derived(const Derived&);
    explicit Derived(Derived&&);
    explicit Derived(const Base&);
    Derived(Base&&);
};

Derived foo() {
  Derived result;
  return result;
}

int main() {
  Derived result = foo();
}

Clang(用3.5测试)拒绝它,并显示以下错误信息:

test.cpp:13:10: error: no matching constructor for initialization of 'Derived'
  return result;
         ^~~~~~
test.cpp:8:5: note: candidate constructor not viable: no known conversion from 'Derived' to 'Base &&' for 1st argument
    Derived(Base&&);
    ^
test.cpp:4:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    Derived();
    ^

谁是对的

我相信Cl在这里是正确的。 GCC不应该接受代码。

原因是在[class.copy] p32(emphasis mine)中指定了在return语句中发生的对象副本的构造函数的重载分辨率:

When the criteria for elision of a copy/move constructor are met,
[…], and the object to be copied is designated by an lvalue, […],
overload resolution to select the constructor for the copy is first
performed as if the object were designated by an rvalue. If the first
overload resolution fails or was not performed, or 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.

在这个例子中,符合elision的标准(通过[class.copy] p31中的第一个项目符号),并且要复制的对象由一个左值指定,因此这一段适用。

首先尝试重载分辨率,就像对象由右值指定一样。显式构造函数不是候选(参见下面的解释为什么),所以选择Derived(Base&&)构造函数。然而,这属于“所选构造函数的第一个参数的类型不是对象类型的r值引用”(相反,它是对象基类类型的r值引用),因此应该再次执行重载分辨率,将对象视为左值。

这个第二个重载解析失败,因为唯一可行的构造函数(再次,显式构造函数不是候选)具有一个rvalue引用参数,它不能绑定到lvalue。 Clang显示所产生的重载分辨率失败错误。

为了完成这个解释,这里是为什么显式构造函数不是重载解析的候选者(所有重点都是我的)。

首先,[dcl.init] p15说:

The initialization that occurs in the = form of a
brace-or-equal-initializer or condition (6.4), as well as in argument
passing, function return, throwing an exception (15.1), handling an
exception (15.3), and aggregate member initialization (8.5.1), is
called copy-initialization
.”

接下来,我们来看看[over.match.ctor] p1:

For copy-initialization, the candidate functions are all the converting
constructors
(12.3.1) of that class.

最后,我们看到明确的构造函数不是在[class.conv.ctor] p1中转换构造函数:

A constructor declared without the function-specifier
explicit
specifies a conversion from the types of its parameters to the type of its class. Such a constructor is called a converting
constructor
.

http://stackoverflow.com/questions/29834308/overload-resolution-difference-between-gcc-and-clang-involving-move-constructor

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c++ 涉及move构造函数和’Derived(Base \u0026\u0026)’构造函数的gcc和clang之间的重载分辨率差异