Boost logo

Boost :

Subject: Re: [boost] [utility] string_ref construction from rvalue reference to string
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2014-08-01 18:46:08


Hi Agustín,

Agustín K-ballo Bergé wrote:

> On 31/07/2014 04:53 a.m., Андрей Давыдов wrote:
>
>> Those errors can be caught at compile time if class "basic_string_ref"
>> defintion would contain lines like following:
>>
>> template<typename Allocator>
>> basic_string_ref(std::basic_string<charT, traits, Allocator> &&) =
>> delete;
>>
>> I cannot understant does it prevent any correct usage of string_ref?
>>
>
> I don't think this is such a good idea. Unless I'm mistaken, it would
> break the typical case of calling a function taking a `string_ref` with a
> temporary.
>
> A `string_ref` does not own the contents it represents, and the only way
> to use it correctly is by guaranteeing that the lifetime of the contents
> extends past that of the reference. You wouldn't generally use
> `string_ref`s as automatic variables or members.
>

I suppose that would break cases like:

std::string getStr();
print(std::string_ref sr) { std::cout << sr << std::endl; }

print(getStr());

Causing people to have to write print(getStr().c_str()).

There doesn't seem to be a good, compile-time way to catch misuse. Your
suggestion on avoiding using string_ref as a class member seems good.

Nate


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