[英]static_cast with an explicit rvalue conversion operator
I am writing a simple wrapper class, and I want to provide explicit conversion operators to the wrapped type. 我正在编写一个简单的包装类,我想为包装类型提供显式转换运算符。 The following code compiles fine with gcc
以下代码使用gcc
编译好
class wrap
{
double value;
public:
explicit wrap(double x) : value(x) {}
explicit operator double&&() && { return std::move(value); }
};
int main() {
wrap w(5);
double && x (std::move(w) ); //ok
double && y = static_cast<double&&>(std::move(w)); //clang reports an error here
}
But clang
reports an error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
但clang
报告error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
. error: cannot cast from lvalue of type 'typename std::remove_reference<wrap &>::type' (aka 'wrap') to rvalue reference type 'double &&'; types are not compatible
。
As far as I know (see the latest draft, 5.2.9 §4 ) static_cast<T>(e)
has the same semantic has T t(e)
, but clang does not refuse the latter. 据我所知(参见最新草案, 5.2.9§4 ) static_cast<T>(e)
具有相同的语义T t(e)
,但clang不拒绝后者。
Which compiler is right? 哪个编译器是对的?
This is clang bug 19917 . 这是clang bug 19917 。 From the section of the standard you mentioned, §5.2.9/4: 从您提到的标准部分,§5.2.9/ 4:
An expression
e
can be explicitly converted to a typeT
using astatic_cast
of the formstatic_cast<T>(e)
if the declarationT t(e);
表达式e
可以显式转换到类型T
使用static_cast
形式static_cast<T>(e)
,如果声明T t(e);
is well-formed, for some invented temporary variablet
. 对于一些发明的临时变量t
。
In this case, T t(e);
在这种情况下, T t(e);
is well-formed and compiles on both compilers, so static_cast<T>(e)
should too. 格式良好并在两个编译器上编译,因此static_cast<T>(e)
应如此。 GCC correctly accepts it. GCC正确接受它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.