fork(3) download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. #if 1 // Not in C++11 // make_index_sequence
  5. #include <cstdint>
  6.  
  7. template <std::size_t...> struct index_sequence {};
  8.  
  9. template <std::size_t N, std::size_t... Is>
  10. struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};
  11.  
  12. template <std::size_t... Is>
  13. struct make_index_sequence<0u, Is...> : index_sequence<Is...> {};
  14.  
  15. #endif // make_index_sequence
  16.  
  17. namespace detail
  18. {
  19. template <typename T, std::size_t...Is>
  20. constexpr std::array<T, sizeof...(Is)> make_array(T val, index_sequence<Is...>)
  21. {
  22. return {(static_cast<void>(Is), val)...};
  23. }
  24. }
  25.  
  26. template <std::size_t N, typename T>
  27. constexpr std::array<T, N> make_array(T val)
  28. {
  29. return detail::make_array(val, make_index_sequence<N>());
  30. }
  31.  
  32. int main()
  33. {
  34. constexpr auto a = make_array<5>(42);
  35.  
  36. for (auto e : a) {
  37. std::cout << e << std::endl;
  38. }
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
42
42
42
42
42