Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <char... Chars>
- struct Foo;
- Foo<"abc">
- Foo<SOME_MACRO("abc")>
- Foo<SOME_MACRO(abc)>
- Foo<'a', 'b', 'c'>
- #include <boost/preprocessor/repetition/repeat.hpp>
- #include <boost/preprocessor/punctuation/comma_if.hpp>
- #define STRING_TO_CHARS_EXTRACT(z, n, data)
- BOOST_PP_COMMA_IF(n) data[n]
- #define STRING_TO_CHARS(STRLEN, STR)
- BOOST_PP_REPEAT_FROM_TO(0, STRLEN, STRING_TO_CHARS_EXTRACT, STR)
- // Not valid, unfortunately, even in C++ 0x :-(
- // Foo<STRING_TO_CHARS(3, "abc")>
- #include <iostream>
- #define E(L,I)
- (I < sizeof(L)) ? L[I] : 0
- #define STR(X, L)
- typename Expand<X,
- cstring<E(L,0),E(L,1),E(L,2),E(L,3),E(L,4), E(L,5),
- E(L,6),E(L,7),E(L,8),E(L,9),E(L,10), E(L,11),
- E(L,12),E(L,13),E(L,14),E(L,15),E(L,16), E(L,17)>
- cstring<>, sizeof L-1>::type
- #define CSTR(L) STR(cstring, L)
- template<char ...C> struct cstring { };
- template<template<char...> class P, typename S, typename R, int N>
- struct Expand;
- template<template<char...> class P, char S1, char ...S, char ...R, int N>
- struct Expand<P, cstring<S1, S...>, cstring<R...>, N> :
- Expand<P, cstring<S...>, cstring<R..., S1>, N-1>{ };
- template<template<char...> class P, char S1, char ...S, char ...R>
- struct Expand<P, cstring<S1, S...>, cstring<R...>, 0> {
- typedef P<R...> type;
- };
- template<char ...S>
- struct Test {
- static void print() {
- char x[] = { S... };
- std::cout << sizeof...(S) << std::endl;
- std::cout << x << std::endl;
- }
- };
- template<char ...C>
- void process(cstring<C...>) {
- /* process C, possibly at compile time */
- }
- int main() {
- typedef STR(Test, "Hello folks") type;
- type::print();
- process(CSTR("Hi guys")());
- }
- #define CHAR_SPLIT(...) #@__VA_ARGS__
- template <char... Chars> class CharTuple {};
- template <typename Tuple, char ch> class Cons;
- template <char... Chars, char ch> class Cons<CharTuple<Chars... ch>> {
- typedef CharTuple<ch, Chars...> type;
- }
- template <bool Condition, typename TrueType, typename FalseType> class If {
- typedef typename TrueType::type type;
- };
- template <typename TrueType, typename FalseType> class If<False> {
- typedef typename FalseType::type type;
- };
- template <typename T> class Identity {
- typedef T type;
- };
- template <char* str> class StringToChars {
- typedef typename If<*str == '