Why for_each returns a function by moving

I read the documentation for std::for_eachhere http://en.cppreference.com/w/cpp/algorithm/for_each and saw that the return valuestd::move(f)

Why does the standard force an input parameter to return value? Could this be changed by default, since the input parameter is passed by value?


This leads me to the following example when you compile the following code

Something function(Something something) {
    return something;
} 
  • The return statement is moving my system with the highest level of optimization ( -O3), why don't most compilers return this return value? Local values ​​are rejected, but function arguments are not ...

  • Does C ++ 17 use elite in this case? I read the sentence ( http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0135r0.html ), but I don’t quite understand what cases are suitable for a mandatory elixir.

I tried this on Apple LLVM version 8.0.0 (clang-800.0.42.1)on my Mac and on g++ 5.4on Ubuntu 16.04.

+6
source share
1 answer

This is due to a late change to the move-semantics rules for C ++ 11. The original move proposal did not automatically move when the arguments to the value-value function appeared in the return statement. However, by the end of the C ++ 11 process, this language feature was added.

for_each " ". . , , ++ 11 .

LWG- 2747 ++ 17.

, , , , ( ), , , , , , - , .

: , ++ 17 . , ++ 11, , for_each .

:

, , ?

N4660, ++ 17, ++ 98/03/11/14... , . . N4659 ( ):

15.8.3 / elision [class.copy.elision]

  • , / ,...

    • return , ( , ( 18.3)) ( cv-qualification) , / ,

.

+11

Source: https://habr.com/ru/post/1016244/


All Articles