Binding temporary to a lvalue reference

后端 未结 5 1509
清酒与你
清酒与你 2020-12-06 12:15

I have the following code

string three()
{
    return \"three\";
}

void mutate(string& ref)
{
}

int main()
{
    mutate(three()); 
    return 0;
}
         


        
5条回答
  •  不知归路
    2020-12-06 12:37

    I guess it depends on the compiler. g++ 4.1.2 gives me this.

    In function 'int main()':
    Line 15: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
    compilation terminated due to -Wfatal-errors.
    

    Maybe because you're not doing anything the call is optimized away.

提交回复
热议问题