英文:
Why is a std::move of a value from a const std::optional possible?
问题
以下是翻译好的部分:
这段代码之所以是合法的C++代码,纯粹是一个理论问题,我没有特定的用例:
#include <optional>
#include <vector>
#include <iostream>
int main() {
std::vector<int> v{1, 2, 3};
const std::optional<std::vector<int>> ov = v;
const auto nv = std::move(ov.value());
for (const auto& x : *ov) { std::cout << x; }
for (const auto& x : nv) { std::cout << x; }
}
这产生了123123
,但我不理解原因。
- 为什么对
const optional
的值应用std::move
是合法的? - 为什么
optional ov
仍然保持着vector
?
ov.value()
内部是否创建了一个临时副本,然后从中移动了呢?
英文:
Why is the following snippet legal C++ code? This is a purely theoretical question - there is no usecase that I have in mind:
#include <optional>
#include <vector>
#include <iostream>
int main() {
std::vector<int> v{1, 2, 3};
const std::optional<std::vector<int>> ov = v;
const auto nv = std::move(ov.value());
for (const auto& x : *ov) { std::cout << x; }
for (const auto& x : nv) { std::cout << x; }
}
This yields 123123
, but I do not understand the reason.
- Why is a
std::move
applied to a value of aconst optional
legal? - Why does the
optional ov
still hold thevector
?
Does ov.value()
internally create a copy to a temporary which is then moved from?
答案1
得分: 18
"Why is std::move applied to a value of a const optional legal?"
为什么将std::move应用于const optional的值是合法的?
"Sure, why not? All that std::move
is, is a cast to an rvalue reference. You'll just end up with an rvalue reference to a const
object. That's nothing unusual."
当然可以,为什么不行呢?std::move
只是将其强制转换为右值引用。您最终会得到对const
对象的右值引用。这并不奇怪。
"Why does the optional ov still holds the vector?"
为什么可选项ov仍然保留着向量?
"Because it is const, and cannot be changed."
因为它是const,不能被更改。
Several conditions must happen in order for an object to be moved. std::move takes care of the most important one, but all others must be satisfied as well.
为了移动对象,必须满足一些条件。std::move处理了最重要的条件,但其他条件也必须满足。
It is a common misconception that spraying std::move everywhere guarantees a super-duper-efficient move of an object from one place to another, by magic. That's not true, that's not what std::move is, it's just a cast that's sometimes needed in order to move something. But that's not everything that's needed, just the most important part.
广泛使用std::move并不保证通过魔法将对象从一个地方移动到另一个地方,这是一个常见的误解。这不是真的,这不是std::move的作用,它只是有时需要的一种类型转换,以便移动某些东西。但这并不是所有需要的,只是最重要的部分。
英文:
> Why is std::move applied to a value of a const optional legal?
Sure, why not? All that std::move
is, is a cast to an rvalue reference. You'll just end up with an rvalue reference to a const
object. That's nothing unusual.
> Why does the optional ov still holds the vector?
Because it is const
, and cannot be changed.
Several conditions must happen in order for an object to be moved. std::move
takes care of the most important one, but all others must be satisfied as well.
It is a common misconception that spraying std::move
everywhere guarantees a super-duper-efficient move of an object from one place to another, by magic. That's not true, that's not what std::move
is, it's just a cast that's sometimes needed in order to move something. But that's not everything that's needed, just the most important part.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论