fork(1) download
  1. #include <iostream>
  2. #include <array>
  3.  
  4. // related: https://stackoverflow.com/questions/17388317/c11-implicit-conversion-from-initialization-list-to-array-parameter
  5.  
  6. template<int V>
  7. struct Cls {int val(){return V;} };
  8.  
  9.  
  10.  
  11. template <int V>
  12. auto make(std::array<const size_t, V> shape) -> Cls<V>{
  13. return Cls<V>();
  14. }
  15.  
  16. // unfortunately, this doesn't work either:
  17. // template <int V>
  18. // constexpr size_t arr_len(std::array<const size_t, V>){return V;}
  19. // template <int V>
  20. // auto make(std::array<const size_t, V> shape) -> Cls<arr_len(shape)>{
  21. // return Cls<arr_len(shape)>();
  22. // }
  23.  
  24.  
  25. int main(int argc, char const *argv[])
  26. {
  27. // I would like to write the following and the compiler should figure out V=2
  28. // auto t0 = make({2, 3}); // doesn't work
  29. // auto t0 = make(2, 3); // would be also ok
  30. auto t1 = make<2>({2, 3}); // does work but need the information twice
  31. std::cout << t1.val() << std::endl;
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
2