static_cast使用显式右值转换运算符
static_cast with an explicit rvalue conversion operator
我正在编写一个简单的包装类,我想为包装类型提供显式转换运算符。以下代码编译良好,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
}
但是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
.
据我所知(见最新草案,5.2.9 §4),static_cast<T>(e)
具有相同的语义T t(e)
,但 clang 并不拒绝后者。
哪个编译器是正确的?
这是 clang bug 19917。从您提到的标准部分 §5.2.9/4:
表达式
e
可以使用表单static_cast<T>(e)
的static_cast
显式转换为类型T
如果声明T t(e);
格式正确,对于某些发明的临时变量t
。
在这种情况下,T t(e);
格式正确,并且可以在两个编译器上进行编译,因此static_cast<T>(e)
也应该如此。海湾合作委员会正确地接受了它。
相关文章:
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 构造函数和转换运算符之间的重载解析
- 分配给转换运算符失败-C++
- 转换运算符不适用于sleep_until
- 继承模板化转换运算符
- 模板转换运算符在 clang 6 和 clang 7 之间的区别
- 如何在模板化转换运算符中消除此构造的歧义?
- 为什么选择转换运算符的重载?
- 如何避免强制转换运算符 () 和访问运算符 [] 冲突?
- 如果可能的话,C++总是更喜欢右值引用转换运算符而不是常量左值引用吗?
- 了解转换运算符的选择C++
- 多个隐式转换运算符
- 这个typedef和转换运算符语法是什么意思
- 为什么转换运算符调用复制构造函数两次,而等效函数只调用它一次
- 类模板忽略了用户定义的转换运算符(非模板不忽略)
- 为什么在std::for_each()返回时调用转换运算符
- 为什么 std::optional 的强制转换运算符被忽略了
- 使用用户定义的转换运算符推导函数模板参数
- 模板转换运算符的分辨率不明确
- 统一初始化是隐式发生的,即使 int 强制转换运算符是使用 explicit 关键字声明的.原因是什么?