为什么可以从const std::optional中std::move一个值?

huangapple go评论63阅读模式
英文:

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,但我不理解原因。

  1. 为什么对const optional的值应用std::move是合法的?
  2. 为什么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 &lt;optional&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;

int main() {
    std::vector&lt;int&gt; v{1, 2, 3};
    const std::optional&lt;std::vector&lt;int&gt;&gt; ov = v;
    const auto nv = std::move(ov.value());

    for (const auto&amp; x : *ov) { std::cout &lt;&lt; x; }
    for (const auto&amp; x : nv) { std::cout &lt;&lt; x; }
}

This yields 123123, but I do not understand the reason.

  1. Why is a std::move applied to a value of a const optional legal?
  2. Why does the optional ov still hold the vector?

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.

huangapple
  • 本文由 发表于 2023年5月10日 18:22:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定