fork download
  1. #include <iostream>
  2. #include <cstddef>
  3.  
  4. template <char...>
  5. struct string
  6. {
  7. static const char c_str[];
  8. };
  9.  
  10. template <char... S>
  11. const char string<S...>::c_str[] = { S..., 0 };
  12.  
  13. template <typename T>
  14. struct valdef
  15. {
  16. typedef T value;
  17. };
  18.  
  19. template <std::size_t...>
  20. struct indices
  21. {};
  22.  
  23. template <std::size_t, typename>
  24. struct push_back;
  25.  
  26. template <std::size_t I, std::size_t... II>
  27. struct push_back<I, indices<II...>>
  28. : valdef<indices<II..., I>>
  29. {};
  30.  
  31. template <std::size_t N>
  32. struct make_indices_impl
  33. : valdef
  34. <
  35. typename push_back
  36. <
  37. N,
  38. typename make_indices_impl<N - 1>::value
  39. >::value
  40. >
  41. {};
  42.  
  43. template <>
  44. struct make_indices_impl<0>
  45. : valdef<indices<0>>
  46. {};
  47.  
  48. template <std::size_t N>
  49. struct make_indices
  50. : valdef<typename make_indices_impl<N - 1>::value>
  51. {};
  52.  
  53. template <>
  54. struct make_indices<0>
  55. : valdef<indices<>>
  56. {};
  57.  
  58. template <char const*, typename>
  59. struct to_meta_string_impl;
  60.  
  61. template <char const* S, std::size_t... I>
  62. struct to_meta_string_impl<S, indices<I...>>
  63. : valdef<string<S[I]...>>
  64. {};
  65.  
  66. template <char const* S, std::size_t N>
  67. struct to_meta_string
  68. : valdef
  69. <
  70. typename to_meta_string_impl
  71. <
  72. S,
  73. typename make_indices<N>::value
  74. >::value
  75. >
  76. {};
  77.  
  78. struct static_string
  79. {
  80. static constexpr char value[];
  81. };
  82.  
  83. constexpr char static_string::value[] = "abcdef";
  84.  
  85. int main()
  86. {
  87. std::cout << to_meta_string<static_string::value, 6>::value::c_str;
  88. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:63:18: error: template parameter 'S' of type 'const char*' is not allowed in an integral constant expression because it is not of integral or enumeration type
prog.cpp:63:21: error: an array reference cannot appear in a constant-expression
prog.cpp:63:22: error: expected parameter pack before '...'
prog.cpp:63:25: error: template argument 1 is invalid
prog.cpp: In instantiation of 'to_meta_string<((const char*)(& static_string::value)), 6u>':
prog.cpp:87:54:   instantiated from here
prog.cpp:76:1: error: no type named 'value' in 'struct to_meta_string_impl<((const char*)(& static_string::value)), indices<0u, 1u, 2u, 3u, 4u, 5u> >'
prog.cpp: In function 'int main()':
prog.cpp:87:56: error: 'to_meta_string<(const char*)&static_string::value, 6u>::value' has not been declared
stdout
Standard output is empty