Boost logo

Boost :

Subject: Re: [boost] Heads up - string_ref landing
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2012-11-19 08:07:37

On Mon, Nov 19, 2012 at 4:49 PM, Olaf van der Spek <ml_at_[hidden]> wrote:
> On Mon, Nov 19, 2012 at 4:42 AM, Yanchenko Maxim
> <maximyanchenko_at_[hidden]> wrote:
>> Mathias, it's not just compile-time size (which is good to have as well, btw).
>> What about literals with a zero character inside, like "foo\0bar"?
> What about them?
> Such literals aren't proper null-terminated strings, are they?
>> I believe string_ref should be able to handle the case, in order to be useful.
> For such literals the (const char*, size_t) constructor should be used.

This is simply not practical:

// code duplication
string_ref lit("Hello, World!", sizeof("Hello, World!"));
wstring_ref lit(L"Hello, World!", sizeof(L"Hello, World!") / sizeof(wchar_t));
// error prone
string_ref lit("Hello, World!", 13);

Although I don't aim to address the \0 in the middle of the string
literal, I'd like such construction to be cleaner and preferably
without strlen/wcslen:

string_ref lit = string_ref::from_literal("Hello, World!");


string_ref lit = make_literal_ref("Hello, World!");

or whatever. Not every compiler is able to optimize away strlen/wcslen
on a literal, this generator is intended to remove its need

PS: I don't really understand why we are arguing about this little
helper anyway. There are people who will find it useful, others can
safely ignore it.

Boost list run by bdawes at, gregod at, cpdaniel at, john at