Boost logo

Boost :

Subject: Re: [boost] Compile Time String in C++11/C++14
From: Adam Wulkiewicz (adam.wulkiewicz_at_[hidden])
Date: 2014-08-26 07:43:17


Tongari J wrote:
> Hi Louis,
>
> 2014-08-26 0:01 GMT+08:00 Louis Dionne <ldionne.2_at_[hidden]>:
>> This is genius, thanks a lot for posting. I've been searching for a
>> way to do this to implement compile-time strings in Boost.Hana[1].
>>
> Glad to hear :)
>
> Actually I've found an even simpler way:

Nice! And with some small tweaks it works in C++11, assuming you have
some implementation of index_sequence:

```
     template<char...>

     struct str {};

     template<std::size_t... Ns, class S>

     auto make_str_impl(index_sequence<Ns...>, S) -> decltype(str<S::get()[Ns]...>())

     {

          return str<S::get()[Ns]...>();

     }

     template<class S>

     auto make_str(S) -> decltype(make_str_impl(make_index_sequence<sizeof(S::get()) - 1>(), S()))

     {

          return make_str_impl(make_index_sequence<sizeof(S::get()) - 1>(), S());

     }

     #define LIT(s) make_str([]() \

     { \

          struct \

          { \

              static constexpr decltype(s) get() \

              { \

                  return s; \

              } \

          } _; \

          return _; \

     }())

```

Well VS2013 complains about a static data member in locally defined
class but I assume it's a VS bug.

Regards,
Adam


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