c ++ 11:如何理解函数move [英] c++11: how to understand the function move

查看:323
本文介绍了c ++ 11:如何理解函数move的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解c ++ 11中的函数move。

I can't understand the function move in c++11.

这里,我得到了以下的东西:

From here, I got things below:


标准库 - 移动意味着
移动对象保留在有效但未指定的状态。其中
意味着,在这样的操作之后,移动的
对象的值应该只被销毁或分配一个新值
;访问它
否则产生一个未指定的值。

Although note that -in the standard library- moving implies that the moved-from object is left in a valid but unspecified state. Which means that, after such an operation, the value of the moved-from object should only be destroyed or assigned a new value; accessing it otherwise yields an unspecified value.

在我看来,move已清除。但是,我已经做了以下测试:

In my opinion, after move(), the moved-from object has been "clear". However, I've done a test below:

std::string str = "abcd";
std::move(str);
std::cout<<str;

我在我的屏幕上得到 abcd br>
所以有 str 被销毁了吗?如果是这样,我可以得到 abcd ,因为我只是幸运?或者我误解了函数 move

I got abcd on my screen.
So has the str been destroyed? If so, I could get abcd because I'm just lucky? Or I misunderstood the function move?

此外,当我阅读 C ++ Primer 我有这样的代码:

Besides, when I read C++ Primer, I got such a code:

class Base{/* ... */};
class D: public Base{
public:
    D(D&& d): Base(std::move(d)){/* use d to initialize the members of D */}
};

现在我很困惑。如果函数 move 将清除该对象,参数 d 将会清楚,我们如何使用d来初始化D的成员?

I'm confused now. If the function move will clear the object, the parameter d will be clear, how could we "use d to initialize the members of D"?

推荐答案

std :: move不会移动任何东西。 std :: move只是一个执行cast的函数模板。 std :: move无条件地将其参数转换为右值,

std::move doesn’t move anything. std::move is merely a function template that perform casts. std::move unconditionally casts its argument to an rvalue,

std::move(str);

使用这个表达式,你只需要执行从lvalue到rvalue的类型转换。

With this expression you are just doing type cast from lvalue to rvalue.

程序中的小修改可以更好地了解。

small modification in program to understand better.

 std::string str = "abcd";
 std::string str1 = std::move(str);
 std::cout<<str<<std::endl;
 std::cout<<str1<<std::endl; 

str lvalue typecast to rvalue by std :: move std :: string = std :: move(str); => c $ c> string move constructor 其中实际窃取资源。 str 资源(abcd)被钢化并打印为空字符串。

str lvalue typecast to rvalue by std::move, std::string = std::move(str); =>this expression call the string move constructor where actual stealing of resources take placed. str resources(abcd) are steeled and printed empty string.

这里是move函数的示例实现。请注意,这不是标准库的完整实现。

Here is sample implementation of move function. Please note that it is not complete implementation of standard library.

template<typename T> // C++14; still in
decltype(auto) move(T&& param) // namespace std
{
using ReturnType = remove_reference_t<T>&&;
return static_cast<ReturnType>(param);
}

将std :: move应用到对象告诉编译器对象是合格的被移动。它投向了右值。

Applying std::move to an object tells the compiler that the object is eligible to be moved from. It cast to the rvalue.

class Base{/* ... */};
class D: public Base{
public:
    D(D&& d): Base(std::move(d)){/* use d to initialize the members of D */}
};

Base(std :: move(d))它会做up-casting只移动基类部分。

Base(std::move(d)) it will do up-casting only move the base class part only.

这里有一个有趣的事情为你学习。如果你没有使用 std :: move D(D& d)调用基类析构函数:Base(d)那么d将被视为Base类的lvalue和copy构造函数,而不是move构造函数。详情请参阅在导出对象上移动构造函数

Here one more interesting thing to learn for you. If you do not invoke base class destructor with std::move like D(D&& d): Base(d) then d will be considered as lvalue and copy constructor of Base class involved instead of move constructor. Refer for more detail Move constructor on derived object

这篇关于c ++ 11:如何理解函数move的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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