为什么 <algorithm> 和 <utility> 中都有 std::move

Why is there a std::move in both <algorithm> and <utility>

大多数时候我在这里看到 std::move,它引用的是 <utility> 版本。

std::move in <algorithm> actually does what its name suggests, move, whereas the std::move in <utility> 将其参数转换为一个 xvalue,这基本上只是一个预处理步骤,最终将 xvalue 移动到一个 lvalue。那么当它们的功能不同时,将它们都命名为 move 是不是有点令人困惑?

So isn't it kind of confusing for both of these to be named move?

这可能会造成混淆,尤其是那些不习惯支持重载的语言的人。确实,编程指南通常不鼓励具有不同含义的重载。

但也不是很难知道有两个同名函数,尽管这是主观的。不同的参数列表提供了足够的上下文来轻松识别彼此。

Why is there a std::move in both and

因为语言的设计者选择为两个函数使用相同的名称。

std::move 是一种非常简洁的方式来表达这两个功能。 <algorithm> 中的一个与 header 中的 std::copy 互补。

whereas the std::move in casts its argument to an xvalue, which is basically just a preprocessing step for eventually moving

描述函数的作用并不是函数名所能表达的唯一含义。在这种情况下,名称表达了使用该函数的程序员的意图:程序员打算从参数左值移动 - 如果可能的话。

这是程序员可能需要经常编写的内容;因此需要非常短的名称。