Warn if accessing moved object in C++11 [duplicate]

随声附和 提交于 2019-12-01 02:30:36
Nicol Bolas

Nor should they. The behavior of a moved-from class is whatever you want it to be. It is not something that a compiler should be warning about.

For standard library objects, a moved-from class is in a "valid but unspecified state". As such, it is perfectly legal to do this:

std::vector<int> v{20, 30, 40};
std::vector<int> v2 = std::move(v);
v = std::vector<int>{50, 60, 70, 80};

clear doesn't care what the current state of the vector is; it just clears the vector. Thus it is reset to a known state. Similarly, operator= doesn't care what the current state is; it will reset it to a known state.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!