C ++右值引用和移动语义 [英] C++ Rvalue references and move semantics

查看:162
本文介绍了C ++右值引用和移动语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的C ++ 03有可能隐含发生不必要的复制的问题。为此,C ++ 11引入了右值引用移动语义。现在的问题是,这样做不必要的复制问题也存在于语言,如C#和Java还是仅仅一个C ++的问题?换句话说,确实右值引用作相比,C#或Java?

C++03 had the problem of unnecessary copies that could happen implicitly. For this purpose, C++11 introduced rvalue references and move semantics. Now my question is, do this unnecessary copying problem also exist in languages such as C# and java or was it only a C++ problem? In other words, does rvalue references make C++11 even more efficient as compared to C# or Java?

C ++ 11更有效率至于C#而言(运算符重载允许的话),可以说我们有一个数学向量类,我们用它这个样子。

As far as C# concerned (operator overloading allowed in it), lets say we have a mathematical vector class, and we use it like this.

vector_a = vector_b + vector_c;



编译器必将变换 vector_b + vector_c 一些临时对象(可以称之为 vector_tmp )。

The compiler will surely transform vector_b + vector_c to some temporary object (lets call it vector_tmp).

现在我不认为C#可以一个区分临时右值,如 vector_tmp vector_b A左值这样的,所以我们必须将数据复制到 vector_a 反正,它可以轻松地通过使用右值引用移动语义来避免在C ++ 11。

Now I don't think C# can differentiate between a temporary rvalue such as vector_tmp or a an lvalue such as vector_b, so we'll have to copy data to vector_a anyway, which can easily be avoided by using rvalue references and move semantics in C++11.

推荐答案

在C#和Java类引用具有的一些特性的shared_ptr S IN C ++。然而,右值引用和移动语义更多地涉及到临时值类型,但相比于C ++值类型在C#中的值类型是相当不灵活,从我自己的C#的经验,你会带类,而不是结构,结束了最的时间。

Class references in C# and Java have some properties of shared_ptrs in C++. However, rvalue references and move semantics relate more to temporary value types, but the value types in C# are quite non-flexible compared to C++ value types, and from my own C# experience, you'll end up with classes, not structs, most of the time.

所以,我的假设是,无论是Java和C#也多来自这些新的C ++功能,它可以让代码做到安全假设的东西是否是暂时的利润,而而不是复制让它只是窃取的内容。

So my assumption is that neither Java nor C# would profit much from those new C++ features, which lets code make safe assumptions whether something is a temporary, and instead of copying lets it just steal the content.

这篇关于C ++右值引用和移动语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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