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

Iamanon :

Most times I see std::move posted on here, it's referencing the <utility> version.

The std::move in <algorithm> actually does what its name suggests, move, whereas the std::move in <utility> casts its argument to an xvalue, which is basically just a preprocessing step for eventually moving the xvalue into an lvalue. So isn't it kind of confusing for both of these to be named move when the functionality for each is different?

eerorika :

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

It can be confusing, especially those who are not used to languages that support overloading. It is true that programming guidelines typically discourage overloads with separate meanings.

But it is also not very difficult to learn that there are two functions by the same name, although this is subjective. The different argument lists provide sufficient context to easily recognise one from the other.

Why is there a std::move in both and

Because the designers of the language chose to use the same name for both functions.

std::move is a very concise way to express both functions. The one in <algorithm> is complementary to std::copy from the same header.

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

Describing what the function does is not the only thing that the name of the function can express. In this case, the name expresses the intention of the programmer who used the function: The programmer intends to move from the argument lvalue - if possible.

This is something that programmers may potentially need to write quite often; thus there is a need for very short name.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=368942&siteId=1
Recommended