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