samedi 1 avril 2017

Why does for_each return function by move

I was reading the documentation for std::for_each here http://ift.tt/1avWIYr and saw that the return value is std::move(f)

Why does the standard enforce moving the input parameter in the return value? Won't it be moved by default anyway, since the input parameter is passed by value?


This leads me to a followup, when you compile the following code

Something function(Something something) {
    return something;
} 

The return statement is a move on my system with the highest optimization level (-O3), why don't most compilers elide this return value? Does C++17 enforce elision in this case? I read the proposal (http://ift.tt/1JFBM18) but I do not fully understand which cases qualify for mandatory elision.

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

Aucun commentaire:

Enregistrer un commentaire