daily pastebin goal
11%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. template <char... Chars>
  2. struct Foo;
  3.    
  4. Foo<"abc">
  5.    
  6. Foo<SOME_MACRO("abc")>
  7.    
  8. Foo<SOME_MACRO(abc)>
  9.    
  10. Foo<'a', 'b', 'c'>
  11.    
  12. #include <boost/preprocessor/repetition/repeat.hpp>
  13. #include <boost/preprocessor/punctuation/comma_if.hpp>
  14.  
  15. #define STRING_TO_CHARS_EXTRACT(z, n, data)
  16.     BOOST_PP_COMMA_IF(n) data[n]
  17.  
  18. #define STRING_TO_CHARS(STRLEN, STR)  
  19.     BOOST_PP_REPEAT_FROM_TO(0, STRLEN, STRING_TO_CHARS_EXTRACT, STR)
  20.  
  21. // Not valid, unfortunately, even in C++ 0x :-(
  22. // Foo<STRING_TO_CHARS(3, "abc")>
  23.    
  24. #include <iostream>
  25.  
  26. #define E(L,I)
  27.   (I < sizeof(L)) ? L[I] : 0
  28.  
  29. #define STR(X, L)                                                      
  30.   typename Expand<X,                                                    
  31.                   cstring<E(L,0),E(L,1),E(L,2),E(L,3),E(L,4), E(L,5),  
  32.                           E(L,6),E(L,7),E(L,8),E(L,9),E(L,10), E(L,11),
  33.                           E(L,12),E(L,13),E(L,14),E(L,15),E(L,16), E(L,17)>
  34.                   cstring<>, sizeof L-1>::type
  35.  
  36. #define CSTR(L) STR(cstring, L)
  37.  
  38. template<char ...C> struct cstring { };
  39.  
  40. template<template<char...> class P, typename S, typename R, int N>
  41. struct Expand;
  42.  
  43. template<template<char...> class P, char S1, char ...S, char ...R, int N>
  44. struct Expand<P, cstring<S1, S...>, cstring<R...>, N> :
  45.   Expand<P, cstring<S...>, cstring<R..., S1>, N-1>{ };
  46.  
  47. template<template<char...> class P, char S1, char ...S, char ...R>
  48. struct Expand<P, cstring<S1, S...>, cstring<R...>, 0> {
  49.   typedef P<R...> type;
  50. };
  51.    
  52. template<char ...S>
  53. struct Test {
  54.   static void print() {
  55.     char x[] = { S... };
  56.     std::cout << sizeof...(S) << std::endl;
  57.     std::cout << x << std::endl;
  58.   }
  59. };
  60.  
  61. template<char ...C>
  62. void process(cstring<C...>) {
  63.   /* process C, possibly at compile time */
  64. }
  65.  
  66. int main() {
  67.   typedef STR(Test, "Hello folks") type;
  68.   type::print();
  69.  
  70.   process(CSTR("Hi guys")());
  71. }
  72.    
  73. #define CHAR_SPLIT(...) #@__VA_ARGS__
  74.    
  75. template <char... Chars> class CharTuple {};
  76.    
  77. template <typename Tuple, char ch> class Cons;
  78. template <char... Chars, char ch> class Cons<CharTuple<Chars... ch>> {
  79.     typedef CharTuple<ch, Chars...> type;
  80. }
  81.    
  82. template <bool Condition, typename TrueType, typename FalseType> class If {
  83.     typedef typename TrueType::type type;
  84. };
  85. template <typename TrueType, typename FalseType> class If<False> {
  86.     typedef typename FalseType::type type;
  87. };
  88.    
  89. template <typename T> class Identity {
  90.     typedef T type;
  91. };
  92.  
  93. template <char* str> class StringToChars {
  94.     typedef typename If<*str == '', Identity<CharTuple<>>,
  95.                         Cons<*str, typename StringToChars<str + 1>::type>>::type type;
  96. };
  97.    
  98. template <char... Chars> class Foo { /* ... */ };
  99.    
  100. template <typename> class FooImpl;
  101. tempalte <char... Chars> class FooImpl<CharTuple<Chars...>> { /* ... */ };
  102.    
  103. template <char* str> class Foo {
  104.     typedef typename FooImpl<typename StringToChars<str>::type>::type type;
  105. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top