c++ - Returning std::move(f) in std::for_each -


i'm writing implementation of standard c++ library study.

the c++11 standard says for_each returns std::move(f).

template <class inputiterator, class function> function for_each(inputiterator first, inputiterator last, function f);  returns: std::move(f). 

i thought function scope local variable move-constructed when it's returned. should return move(f) explicitly?

from josuttis 's the c++ standard library

you don’t have , should not move() return values. according language rules, standard specifies following code

x foo () { x x; ...  return x; } 

the following behavior guaranteed:

• if x has accessible copy or move constructor, compiler may choose elide copy. so-called (named) return value optimization ((n)rvo), specified before c++11 , supported compilers.

• otherwise, if x has move constructor, x moved.

• otherwise, if x has copy constructor, x copied.

• otherwise, compile-time error emitted.

from §25.2.4 (for_each)

requires:function shall meet requirements of moveconstructible (table 20). [note:function need not meet requirements of copyconstructible (table 21).—end note]

with std::move(f) can guaranteed of being able read mutated state externally.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -