Boost logo

Boost :

Subject: Re: [boost] [gsoc16] Can I quickly check if the below really is the best approach?
From: Agustín K-ballo Bergé (kaballo86_at_[hidden])
Date: 2016-01-14 13:09:44


On 1/14/2016 2:59 PM, Nevin Liber wrote:
> On 14 January 2016 at 11:54, Agustín K-ballo Bergé <kaballo86_at_[hidden]>
> wrote:
>
>> On 1/14/2016 2:45 PM, Gottlob Frege wrote:
>>
>>> On Wed, Jan 13, 2016 at 1:56 PM, Nevin Liber <nevin_at_[hidden]>
>>> wrote:
>>>
>>>> On 13 January 2016 at 12:25, Lee Clagett <forum_at_[hidden]> wrote:
>>>>
>>>> The macro `STRING_VIEW` seems unnecessary because the `string_view`
>>>>> constructor taking a single NULL-terminated string is also
>>>>> `constexpr`.
>>>>>
>>>>>
>>>> While the templated constructor for basic_string_view is constexpr, this
>>>> constructor for string_view cannot be used in a constexpr context because
>>>> char_traits<char>::length isn't constexpr. :-( See LWG 2232
>>>> <http://cplusplus.github.io/LWG/lwg-active.html#2232>.
>>>>
>>>> I've been told it works this way by design...
>>>>
>>>> So yes, we do need the macro.
>>>>
>>>
>>> Can it be a constexpr function that returns an initializer list?
>>>
>>
>> No, because initializer lists are backed by arrays of automatic storage,
>> so the returned `initializer_list` would immediately dangle.
>>
>> Or
>>> something, anything,... I hate macros :-(
>>>
>>
>> Yes, templates, as usual:
>>
>> template <typename CharT, std::size_t N>
>> constexpr std::experimental::basic_string_view<CharT>
>> make_string_view(CharT const (&str)[N]) noexcept {
>> return {str, N - 1};
>> }
>>
>> constexpr string_view sv = make_string_view("hello");
>> static_assert(sv.size() == 5);
>>
>
> That does not do the same thing if the literal has embedded '\0' characters
> or if you pass it an array that isn't a '\0'-terminated string.

"The same thing" than what? It does the exact some thing the macro in
this context would do:

     #define STRING_VIEW(str) { str, sizeof(str)-1 }

Regards,

-- 
Agustín K-ballo Bergé.-
http://talesofcpp.fusionfenix.com

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