std :: move()只是强制转换? [英] std::move() is just casting?

查看:60
本文介绍了std :: move()只是强制转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了std :: move()的一些(伪)实现.所有这些都只是舍弃了参数的引用,然后将其作为右值引用返回.

I have read a few (pesudo) implementions of std::move(). And all are just casting away the reference of the parameter and then returning it as a rvalue reference.

没有什么比这更重要的了.

It doesn't do anything more than that.

但是,我很好奇:1.它是否还能做得更多.2. standard是否明确声明要使调用者知道其副作用.

However, I am curious: 1. whether it does more than that. 2. whether standard explicitly states that so that the caller should be aware the side effect.

在《 C ++编程语言》第4版中,它指出"move(x)将x标记为要销毁,因此应谨慎使用move(x)".

In the book The C++ Programming Language 4th edition, it states "move(x) marks x for destruction so that move(x) should be used with care".

这是否意味着该标准具有副作用,因此编译器可以进行某些优化?

Does it mean that there is a side effect imposed by the standard and so compiler can do certain optimization?

谢谢.

推荐答案

是的,这与标准中所描述的完全相同.在 N4659 (最后我找到的草稿)

Yes, that's exactly as it is described in the standard. In N4659 (which is last draft I found)

在§23.2.5中说

模板< class T>constexpr remove_reference_t< T>&move(T& t)noexcept;

返回: static_cast< remove_reference_t< T>&&>(t)

它不会标记任何要破坏的内容,也不会更改对象,但是对象可以在接受rvalue的函数中更改(例如move构造函数,move赋值运算符)

It doesn't mark anything for destruction, and it doesn't change the object but object may be changed in function that accepts rvalue (such as move constructor, move assignment operator)

这篇关于std :: move()只是强制转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆