Boost logo

Boost :

Subject: Re: [boost] Constructing string_ref from rvalue string
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-03-22 22:38:09


On 23/03/2017 03:28, Olaf van der Spek via Boost wrote:
> void f2(const string&);
> string f1();
>
> f1(f2());
>
> Isn't this a very common pattern?

Yes (although you have it reversed), but that's not what we were talking
about. We were talking about this:

     void f1(const string_ref&);
     string f2();

     f1(f2());

Or this:

     void f1(const string_ref&);
     string_ref f2(const string_ref&);
     string f3();

     f1(f2(f3()));

They both *should* be safe because the temporary string should not be
destroyed until after f1 is called. But some old/embedded compilers
would sometimes get this wrong when optimising. You can also get into
trouble even on modern compilers if binding is involved in certain
patterns, since then the full-expression might end before the call
actually happens.

I did make an error in my initial response; I've just been burned by
buggy implementations in the past and it makes me nervous, so I jumped
the gun. I apologise for any confusion.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk