fork(1) download
  1. #include <array>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. template<size_t... Is> struct seq {};
  7. template<size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
  8. template <size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
  9.  
  10. template<typename Func, size_t... Is>
  11. constexpr array<float, sizeof...(Is)> make_coeffs(Func f, seq<Is...>) {
  12. return array<float, sizeof...(Is)>{ f(Is)... };
  13. }
  14.  
  15. constexpr float square(float x) { return x * x; }
  16.  
  17. int main() {
  18. constexpr auto coeffs = make_coeffs(square, gen_seq<10>{});
  19. static_assert(coeffs[3] == 9, "");
  20. for (float x : coeffs) {
  21. cout << x << " ";
  22. }
  23. cout << endl;
  24. }
  25.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0 1 4 9 16 25 36 49 64 81