fork(2) download
  1. #include <cstddef>
  2. #include <array>
  3.  
  4. template< std::size_t ... >
  5. struct index_helper {};
  6.  
  7. template< std::size_t n, std::size_t ... seq >
  8. struct index_helper< n, seq ... >
  9. { typedef typename index_helper< n - 1, n, seq ... >::type type; };
  10.  
  11. template< std::size_t ... seq >
  12. struct index_helper< 0, seq ... >
  13. { typedef index_helper type; };
  14.  
  15. template< std::size_t size >
  16. constexpr std::array< char, size >
  17. string_to_array( char const (&str)[ size ] )
  18. { return string_to_array( str, typename index_helper< size - 2 >::type() ); }
  19.  
  20. template< std::size_t size, std::size_t ... index >
  21. constexpr std::array< char, size >
  22. string_to_array( char const (&str)[ size ],
  23. index_helper< index ... > )
  24. { return {{ str[ ( index + 3 ) % size ] ... }}; }
  25.  
  26. constexpr std::array< char, 14 > x = string_to_array( "hello, world!" );
  27.  
  28. #include <iostream>
  29.  
  30. int main() {
  31. std::cout.write( x.begin(), x.size() );
  32. }
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
lo, world!he