Boost logo

Boost :

Subject: Re: [boost] Compile Time String in C++14
From: Louis Dionne (ldionne.2_at_[hidden])
Date: 2014-08-25 12:01:30


TONGARI J <tongari95 <at> gmail.com> writes:

>
> Hi all,
>
> I found a way to make CT strings in C++14, with the help of generic
> lambda.
>
> FYI, Ábel Sinkovics has presented a c++11 CT string in his
> mpllibs::metaparse[1] lib before, but it requires some PP trick which has
> a
> predefined limit MPLLIBS_LIMIT_STRING_SIZE.
>
> The new trick doesn't suffer from the limitation, but it assumes a
> null-terminated string.
> It's fairly simple, see below:
>
> ```c++
> template<char...>
> struct str {};
>
> template<char c>
> struct ch {};
>
> template<int n>
> using idx = std::integral_constant<int, n>;
>
> template<class S, char c, char... cs>
> auto accum(S s, ch<c>, str<cs...>)
> {
> return accum(s, s(idx<sizeof...(cs) + 1>()), str<cs..., c>());
> }
>
> template<class S, char... cs>
> auto accum(S, ch<0>, str<cs...> ret)
> {
> return ret;
> }
>
> template<class S>
> auto make_str(S s)
> {
> return accum(s, s(idx<0>()), str<>());
> }
>
> #define LIT(s) make_str([](auto idx) { return ch<s[idx.value]>(); })
> ```
>
> Then you can use `doSomething(LIT("abc"))` in your code.
> What do you think?

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].

Louis

[1]: https://github.com/ldionne/hana

--
View this message in context: http://boost.2283326.n4.nabble.com/Compile-Time-String-in-C-14-tp4666747p4666896.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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