Re: [RFC][13/19] Target FP: Perform Ada fixed-point scaling in target format

Simon Marchi
2017-10-09T21:12:45Z

On 2017-10-09 02:09 PM, Joel Brobecker wrote:
>>> This is a C++ question, really. Does it make any difference if you
>>> declare the std::string first, and then only set its contents in
>>> a second statement? I can't remember the details, but it has to do
>>> with initialization vs assignment. I _really_ hope that the string
>>> class is sufficiently well designed that the two are really equivalent
>>> in practice?
>>
>> Huh. Indeed I see worse code when doing the assignment as a separate
>> statement, at least with GCC 4.8. I'll make sure to use initialization
>> instead wherever possible. Thanks for pointing this out!
>
> Let's wait for people who really know better about C++ to tell us
> whether it makes a difference. I was amazed as how careful you have
> to be when using C++ to avoid inefficiencies, but perhaps I am simply
> being paranoid in this case... That's why I tried to phrase this as
> a question.
>

Indeed, it's preferable to use

std::string foo = returns_string ();

than

std::string foo;
foo = returns_string ();

The reason being that in the second case, the default constructor (the
one with no params) is called to construct an empty string, and then that
work is scrapped because we assign a new value. The first form constructs
the string right away with the right content.

While it's preferable to use the first one, the earth wouldn't stop spinning
if you used the second form. In the particular case of std::string, the
default constructor is basically assigning a few fields.

Simon