简体   繁体   English

static_cast,带有显式右值转换运算符

[英]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§4static_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 type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); 表达式e可以显式转换到类型T使用static_cast形式static_cast<T>(e) ,如果声明T t(e); is well-formed, for some invented temporary variable t . 对于一些发明的临时变量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.

相关问题 具有转换运算符的类上的static_cast - static_cast on class with conversion operator 使用static_cast实现转换运算符 - Conversion operator implemented with static_cast 对于static_cast和reinterpret_cast,转换运算符int()失败 - Conversion operator int() failed for static_cast and reinterpret_cast 显式构造函数和static_cast - Explicit constructor and static_cast static_cast与直接调用转换运算符? - static_cast vs. direct call to conversion operator? 为什么static_cast不使用转换运算符指向const? - Why does static_cast not use the conversion operator to pointer to const? C ++强制转换显式类型转换(C样式转换)和static_cast的多种解释 - C++ cast notation of explicit type conversion (C-style cast) and multiple interpretations of static_cast 显式模板转换运算符上的enable_if给出“无效的static_cast” - enable_if on explicit templated cast operator gives “invalid static_cast” 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换) - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) 转换为枚举类型需要显式强制转换(static_cast,C样式强制转换或函数样式强制转换)枚举 - Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast) enum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM