c++ - const-reference binding to a temporary -
consider following:
string const& name1 = get_name(...); string const name2 = get_name(...);
where get_name
returns string
object. known, introduction of move-semantics in c++11, both statements can efficient, first 1 being more since move not need made. (yeah, know return-value optimization, it's more nuanced. general idea.)
however, suppose function calls left out of this:
string const& name3 {"billy"}; string const name4 {"debbie"};
in case, string-literal "billy"
implicitly converted temporary string
, , name3
binds temporary. obviously, name4
not temporary.
is true name3
, name4
, both equally efficient? seems me be...
name3
, name4
indistinguishable rest of program, unless use decltype(name3)
or decltype(name4)
.
so compiler can generate same assembly both cases.
of course, general statement; individual compiler may generate slower or faster code , way find out try on compiler.