Boost logo

Boost :

Subject: Re: [boost] Static constexpr map
From: Boris Rasin (boris_at_[hidden])
Date: 2016-01-14 13:35:54


On 1/13/2016 9:43 PM, Niall Douglas wrote:
>>> #define STRING_VIEW(str) { str, sizeof(str)-1 }
>>> constexpr std::initializer_list<std::pair<const string_view,
>>> weekday>> string_to_weekday {
>>> { STRING_VIEW("sunday"), weekday::sunday },
>>> { STRING_VIEW("monday"), weekday::monday },
>>> { STRING_VIEW("tuesday"), weekday::tuesday },
>>> { STRING_VIEW("wednesday"), weekday::wednesday },
>>> { STRING_VIEW("thursday"), weekday::thursday },
>>> { STRING_VIEW("friday"), weekday::friday },
>>> { STRING_VIEW("saturday"), weekday::saturday }
>>> };
>>>
>> The macro `STRING_VIEW` seems unnecessary because the `string_view`
>> constructor taking a single NULL-terminated string is also
>> `constexpr`. Although, it could reduce the amount of computation
>> done by the C++ interpreter in the compiler.
> Sadly, the current proposal for string_view does not provide a
> default Traits length function which is constexpr for const char *.
> Yes, I find that very daft as a default design choice for a
> string_view, but the macro at least makes it not too irritating.

Perhaps you can get rid of the macro this way:

template <int Len>
constexpr string_view str_view(const char (&s)[Len])
{
     return string_view(s, Len);
}


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