Re: [PATCH v2 5/5] Eliminate make_cleanup_ui_file_delete / make ui_file a class hierarchy

Pedro Alves
2017-01-25T18:37:30Z

On 01/24/2017 07:14 PM, Simon Marchi wrote:

> Oh, I guess it's because buf.string() returns a reference and not a
> string directly. Otherwise, then I guess the compiler could have done
> some return value optimization (look at me, using words I don't
> understand).

I think that if buf.string() returned a copy instead of
a reference, then all the compiler could do in

std::string foo ()
{
string_file buf;
...
return buf.string_copy ();
}

or:

std::string foo ()
{
string_file buf;
std::string tmp;

tmp = buf.string_copy ();
return tmp;
}

is make sure to copy only once, from the buf straight
to the caller. But there would still be one copy. Unless
the compiler is smart enough to understand that buf is dying
at the end of the function and all buf destruction does is
destroy the string, so it could safely move the string
out of buf before buf is destroyed. But I don't think
compilers are that smart, unfortunately.

Thanks,
Pedro Alves