At this point in time, the best we can (portably) do is a macro trick as demonstrated for vtmpl::string
. Basically, we create a list of accesses such as
"abcd" -> {(0 < sizeof "abcd"? "abcd"[0] : 0), (1 < sizeof "abcd"? "abcd"[1] : 0), ...}
…which we trim to obtain the desired result.
The first step is easily done via 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)
Usage of the above looks like this (trimming included):
#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
.
integer_sequence
? This smells like an XY-problem. – Independencestring_literal<N>
. – Miniskirttemplate<typename CharT, CharT... Chars> stuff operator "" _x();
UDLs supported by gcc and clang in the meantime. – Kronfeldconstexpr const char literal[] = "delta"; using X = make_char_sequence<sizeof(literal), literal>;
. Let me know if you're interested in details. – Dispersion