An integer sequence of characters from a user literal taking strings as parameters

Currently, only doubles can create a character pattern in a user-defined literal:

template <char...> double operator "" _x(); // Later 1.3_x; // OK "1.3"_y; // C++14 does not allow a _y user- // defined operator to parse that as a template of chars 

Is there a reasonable way to create a character std::integer_sequence characters using a user-defined literal. In other words, what code _y(const char*, std::size_t) would be such that I ended up with std::integer_sequence<char, '1', '.', '3'> ?

+7
c ++ templates template-meta-programming c ++ 14 user-defined-literals
source share
1 answer

At the moment, the best we can do (portable) is a macro trick, as shown for vtmpl::string . Basically, we create an access list, for example

 "abcd" -> {(0 < sizeof "abcd"? "abcd"[0] : 0), (1 < sizeof "abcd"? "abcd"[1] : 0), ...} 

... which we crop to get the desired result.

The first step is easy to do with BOOST_PP_ENUM , although recursive macros are also fine (definition from here ):

 #define VTMPL_SPLIT_1(s, x, m) m(s, x) #define VTMPL_SPLIT_4(s, x, m) VTMPL_SPLIT_1 (s, x, m), VTMPL_SPLIT_1 (s, x+1 , m), VTMPL_SPLIT_1 (s, x+2 , m), VTMPL_SPLIT_1 (s, x+3 , m) #define VTMPL_SPLIT_16(s, x, m) VTMPL_SPLIT_4 (s, x, m), VTMPL_SPLIT_4 (s, x+4 , m), VTMPL_SPLIT_4 (s, x+8 , m), VTMPL_SPLIT_4 (s, x+12 , m) #define VTMPL_SPLIT_64(s, x, m) VTMPL_SPLIT_16 (s, x, m), VTMPL_SPLIT_16 (s, x+16 , m), VTMPL_SPLIT_16 (s, x+32 , m), VTMPL_SPLIT_16 (s, x+48 , m) #define VTMPL_SPLIT_256(s, x, m) VTMPL_SPLIT_64 (s, x, m), VTMPL_SPLIT_64 (s, x+64 , m), VTMPL_SPLIT_64 (s, x+128, m), VTMPL_SPLIT_64 (s, x+194, m) #define VTMPL_SPLIT_1024(s, x, m) VTMPL_SPLIT_256(s, x, m), VTMPL_SPLIT_256(s, x+256, m), VTMPL_SPLIT_256(s, x+512, m), VTMPL_SPLIT_256(s, x+768, m) 

Using the above looks like this (cropping enabled):

 #define VTMPL_STRING_IMPL(str, n) vtmpl::rtrim<vtmpl::value_list<decltype(*str), VTMPL_SPLIT_##n(str, 0, VTMPL_ARRAY_SPLIT)>>::type # #define VTMPL_STRING(str) VTMPL_STRING_IMPL(str, 64 ) 

Where rtrim is defined in algorithms.hxx .

0
source share

All Articles