fork download
  1. #include <utility>
  2. using namespace std;
  3.  
  4. template <char ... C>
  5. struct string_literal {
  6. typedef string_literal type;
  7. static constexpr const char value[sizeof...(C)] = {C...};
  8. constexpr operator const char* (void) const {
  9. return value;
  10. }
  11. };
  12. template <char ... C>
  13. constexpr const char string_literal<C...>::value[sizeof...(C)];
  14.  
  15. template <unsigned N, const char (&S) [N], typename U>
  16. struct selector;
  17.  
  18. template <unsigned N, const char (&S) [N], unsigned ...I>
  19. struct selector<N, S, index_sequence<I...>> {
  20. using type = string_literal<S[I]...>;
  21. };
  22.  
  23. template <unsigned N, const char (&S) [N]>
  24. struct make_string_literal {
  25. using type = typename selector<N, S, make_index_sequence<N>>::type;
  26. };
  27.  
  28. template <unsigned N>
  29. constexpr auto size(const char (&s) [N]) {
  30. return N;
  31. }
  32.  
  33. extern constexpr char value[] = "test";
  34. constexpr auto literal = make_string_literal<size(value), value>::type{};
  35.  
  36. #include <iostream>
  37. #include <typeinfo>
  38. int main() {
  39. std::cout << typeid(literal).name() << '=' << literal << std::endl;
  40. std::cout << typeid(value).name() << '=' << value << std::endl;
  41. return 0;
  42. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
14string_literalIJLc116ELc101ELc115ELc116ELc0EEE=test
A5_c=test