加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11 move(x)其实意味着static_cast(x)?

发布时间:2020-12-16 05:49:51 所属栏目:百科 来源:网络整理
导读:参见英文答案 When is the move constructor called in the `std::move()` function?2 只是阅读Stroustrup的C编程语言第4版,在第7章他说: move(x) means static_castX(x) where X is the type of x 和 Since move(x) does not move x (it simply produces a
参见英文答案 > When is the move constructor called in the `std::move()` function?2
只是阅读Stroustrup的C编程语言第4版,在第7章他说:

move(x) means static_cast<X&&>(x) where X is the type of x

Since move(x) does not move x (it simply produces an rvalue reference
to x) it would have been better if move() had been called rval()

我的问题是,如果move()只是将变量变成一个rval,实现“移动”引用到变量(通过更新指针)的实际机制是什么?

我以为move()就像一个移动构造函数,除了客户端可以使用move()强制编译器?

解决方法

what is the actual mechanism which achieves the “moving” of the reference to the variable (by updating the pointer)??

将其传递给使用rvalue引用的函数(或构造函数),并从该引用中移动该值.没有转换,变量不能绑定到rvalue引用,所以不能传递给这样一个函数 – 这样可以防止变量被意外移动.

I thought move() is just like a move constructor except the client can use move() to force the compiler??

没有;它被用来将一个左值转换成一个右值,以便将它传递给需要一个右值引用的移动构造函数(或其他移动函数).

typedef std::unique_ptr<int> noncopyable;  // Example of a noncopyable type
noncopyable x;
noncopyable y(x); // Error: no copy constructor,and can't implicitly move from x
noncopyable z(std::move(x)); // OK: convert to rvalue,then use move constructor

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读