【发布时间】:2012-09-28 13:55:21
【问题描述】:
以下代码使用g++ (GCC) 4.7.1 20120721 编译得很好,但是
最近构建 clang version 3.2 (trunk) 失败。
struct Y {};
struct X {
operator const Y() const { return Y(); }
};
void f(Y&& y) {}
int main()
{
f(X());
return 0;
}
将转换运算符更改为operator Y() const 就足够了
使代码在两个编译器上编译。
在这种情况下,哪个编译器实际上是符合标准的?做什么 标准实际上是这样说的吗?
要求的逐字错误:
bla.cpp:14:5: error: no viable conversion from 'X' to 'Y'
f(X());
^~~
bla.cpp:1:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'X' to
'const Y &' for 1st argument
struct Y {
^
bla.cpp:1:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'X' to
'Y &&' for 1st argument
struct Y {
^
bla.cpp:6:3: note: candidate function
operator const Y() const { return Y(); }
^
bla.cpp:10:12: note: passing argument to parameter 'y' here
void f(Y&& y) {}
^
编辑:不幸的是,即使添加了重载
void f(const Y&) {}
仍然让 clang 选择右值引用重载,所以这个 破坏用于编译的现有代码,例如有标准 容器。
【问题讨论】:
-
真的是“
R&&”吗?您可以逐字发布诊断信息吗? -
作为记录,作为一个总的猜测,我会说clang就在这里。
-
@LightnessRacesinOrbit 我放弃了诊断。问题是它没有声明
why转换运算符不适用。 -
一方面,你说你不想在那个对象上使用非常量成员(返回
const Y);另一方面,你说你愿意(要求Y&&)。这里有一些不一致的地方。 -
看起来很清楚。
Y const类型的临时对象无法绑定到Y &&类型的引用。它可以绑定到Y const &&,但这没什么用。
标签: c++ c++11 g++ clang move-semantics