fork(6) download
  1. #include <array>
  2. #include <type_traits>
  3.  
  4. template<int ... N>
  5. struct seq
  6. {
  7. using type = seq<N...>;
  8. static const std::size_t size = sizeof ... (N);
  9.  
  10. template<int I>
  11. struct push_back : seq<N..., I> {};
  12. };
  13.  
  14. template<int N>
  15. struct genseq : genseq<N-1>::type::template push_back<N-1> {};
  16.  
  17. template<>
  18. struct genseq<0> : seq<> {};
  19.  
  20. template<int N>
  21. using genseq_t = typename genseq<N>::type;
  22.  
  23. template<typename T, int...N>
  24. auto repeat(T value, seq<N...>) -> std::array<T, sizeof...(N)>
  25. {
  26. //unpack N, repeating `value` sizeof...(N) times
  27. //note that (X, value) evaluates to value
  28. return {(N, value)...};
  29. }
  30.  
  31. struct A{
  32. A(int){}
  33. };
  34.  
  35.  
  36. template<typename T, size_t N>
  37. std::array<T, N> filled_array(T const& u)
  38. {
  39. //genseq_t<N> is seq<0,1,...N-1>
  40. std::array<T, N> items = repeat(u, genseq_t<N>{} );
  41. return items;
  42. }
  43.  
  44. int main() {
  45. auto x = filled_array<A, 5>(A(1));
  46. }
Success #stdin #stdout 0s 3292KB
stdin
Standard input is empty
stdout
Standard output is empty