7

Attualmente, raddoppia solo in grado di produrre un modello di caratteri in un utente definito letterale:successione di interi di caratteri da letterali stringhe taking definite dall'utente come parametri

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 

C'è un modo intelligente per produrre un std::integer_sequence di caratteri utilizzando un letterale definito dall'utente. In altre parole, quale sarebbe il codice di _y(const char*, std::size_t) in modo che io finisca con uno std::integer_sequence<char, '1', '.', '3'>?

+0

[N3599] (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3599.html) può trasformarsi in C++ 1Z. – Columbo

+1

Hai davvero bisogno di un 'integer_sequence'? Questo ha l'odore di un problema XY. – Columbo

+0

@Columbo Dove hai preso quell'informazione? Infine ho sentito che è stato rifiutato in EWG in favore di un 'string_literal '. –

risposta

0

A questo punto nel tempo, il meglio che possiamo (portabile) fare è un trucco macro come dimostrato for vtmpl::string. Fondamentalmente, creiamo un elenco di accessi come

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

... che abbiamo tagliato per ottenere il risultato desiderato.

Il primo passo è fatto facilmente attraverso BOOST_PP_ENUM, anche se le macro ricorsive sono anche bene (definizione da 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) 

Uso dei look di cui sopra come questo (trimming inclusa):

#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 ) 

Dove rtrim è definito in algorithms.hxx.

Problemi correlati