fork download
  1. #include<array>
  2. #include<cstddef>
  3. #include<utility>
  4.  
  5. using namespace std;
  6.  
  7. template<size_t N> using string_literal_t = char[N];
  8.  
  9. template<class T> struct StrSize; ///< metafunction to get the size of string literal alikes
  10.  
  11. /// specialize StrSize for string literals
  12. template<size_t N>
  13. struct StrSize <string_literal_t<N>>{ static constexpr size_t value = N-1; };
  14.  
  15. /// template variable, just for convenience
  16. template <class T>
  17. constexpr size_t str_size = StrSize<T>::value;
  18.  
  19. /// now do the same but with constexpr function
  20. template<class T>
  21. constexpr auto strsize(const T&) noexcept-> decltype(str_size<T>) {
  22. return str_size<T>;
  23. }
  24.  
  25. template<class S, size_t... Is>
  26. constexpr auto test_helper(const S& s, index_sequence<Is...>) noexcept-> array<char, str_size<S>> {
  27. return {s[Is]...};
  28. }
  29.  
  30. template<class S>
  31. constexpr auto test(const S& s) noexcept-> decltype(auto) {
  32. // return test_helper(s, make_index_sequence<str_size<S>>{}); // this work in both clang and gcc
  33. return test_helper(s, make_index_sequence<strsize(s)>{}); // this works only in gcc
  34. }
  35.  
  36. auto main(int argc, char *argv[])-> int {
  37. static_assert(strsize("qwe") == 3, "");
  38. static_assert(noexcept(test("qwe")) == true, "");
  39.  
  40. return 0;
  41. }
  42.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:33:54: error: non-type template argument is not a constant expression
   return test_helper(s, make_index_sequence<strsize(s)>{});  // this works only in gcc
                                                     ^
prog.cpp:38:27: note: in instantiation of function template specialization 'test<char [4]>' requested here
   static_assert(noexcept(test("qwe")) == true, "");
                          ^
1 error generated.
stdout
Standard output is empty