多数情况下,我在此处看到std::move
的信息,它引用的是<utility>
版本。
std::move
in <algorithm>
实际上按照其名称所建议的那样 move ,而 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
描述函数的功能并不是函数名称可以表达的唯一内容。在这种情况下,该名称表示使用该函数的程序员的意图:程序员打算从参数lvalue移出-如果可能的话。
这是程序员可能需要经常编写的东西。因此,需要非常简短的名称。
关于c++ - 为什么在<algorithm>和<utility>中都存在std::move,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60746803/