vendredi 18 août 2017

Compiler dedication of rvalue-reference for variable going out of scope

Why won't the compiler automatically deduce that a variable is about to go out of scope, and can therefore be considered an rvalue-reference?

Take for example this code:

#include <string>

int foo(std::string && bob);
int foo(const std::string & bob);

int main()
{
    std::string bob("  ");
    return foo(bob);
}

Inspecting the assembly code clearly shows that the const & version of "foo" is called at the end of the function.

Compiler Explorer link here: http://ift.tt/2wYt54q

Aucun commentaire:

Enregistrer un commentaire