为什么 std::move 当类型已经是 T&& 类型时

Why std::move when type is already of the type T&&

本文关键字:类型 std 为什么 move      更新时间:2023-10-16

我今天在一些地方看到人们的回答,说我有Foo的构造函数。

Foo(      std::vector<SomeType>&& data) noexcept : data_(std::move(data)) {};

为什么std::move(data)是必需的,因为std::vector&lt类型的数据不是;SomeType>,,

?

std::move在我看来,它只是将数据强制转换为std::vector,,一遍吗?

http://en.cppreference.com/w/cpp/utility/move定义std::move为,

static_cast<typename std::remove_reference<T>::type&&>(t)

所以看起来std::move在这种情况下是无关的,但我可能遗漏了一些东西。

在这两种情况下,

void foo(const T& bar); // 1
void foo(T&& bar);      // 2

bar实际上是一个lvalue&&的意思是重载2将绑定到右值,例如在以下情况下:

foo(Bar());

而重载1会绑定在这里:

T bar;
foo(bar);

2中,您仍然需要使用std::move来使bar看起来和行为像函数内部的rvalue

相关文章: