std::string &func(int vlu)
{
std::string str;
str = std::to_string(vlu) + "something";
return str;
}
the function above is unsafe clearly.
Following is another version.
std::string &func(int vlu)
{
return std::to_string(vlu) + "something";
}
I have some questions:
the compiler(gcc), in the second version, doesn't give me any warning. Is it safe?
I just think that compiler(or something?) will create a temporary variable to hold the return of expression std::to_string(vlu) + "something"
. So the second version is unsafe too. and I right?
prog.cc:5:12: error: non-const lvalue reference to type 'basic_string<...>' cannot bind to a temporary of type 'basic_string<...>' return std::to_string(vlu) + "something"; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
– Keffiyehstd::string
?? huh? – Scandalizenew
but all stuff you'd posted looks like a bad plan part. – Ives