Subject: Re: [boost] Heads up - string_ref landing
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2012-11-16 10:27:37
On Nov 16, 2012, at 7:00 AM, Maxim Yanchenko <MaximYanchenko_at_[hidden]> wrote:
> Olaf van der Spek <ml <at> vdspek.org> writes:
>> On Fri, Nov 16, 2012 at 3:25 PM, Mathias Gaunard
>> <mathias.gaunard <at> ens-lyon.org> wrote:
>>> void f(std::string const&);
>> Are compilers/optimizers not smart enough to construct the temporary
>> object at compile time?
> I'm afraid they are not smart enough to eliminate an unneeded temporary when
> it's something sophisticated like std::string
Actually, this is a really good example.
void f(std::string const&);
In the call to "f", the compiler will create a temporary std::string. This will involve a call to strlen (possibly done at compile time), a memory allocation (modulo the small-string optimization), and copying the data.
In the call to "g", the compiler will create a temporary string_ref. This will involve a call to strlen (possibly done at compile time).
And for most cases, the code internal to "f" and "g" won't care that they got a string_ref instead of a const std::string.
Marshall Clow Idio Software <mailto:mclow.lists_at_[hidden]>
A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
-- Yu Suzuki