fork download
  1. #include <array>
  2. #include <iostream>
  3. #include <functional>
  4.  
  5. #if 1 // Not in C++11 // make_index_sequence
  6. #include <cstdint>
  7.  
  8. template <std::size_t...> struct index_sequence {};
  9.  
  10. template <std::size_t N, std::size_t... Is>
  11. struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};
  12.  
  13. template <std::size_t... Is>
  14. struct make_index_sequence<0u, Is...> : index_sequence<Is...> {};
  15.  
  16. #endif // make_index_sequence
  17.  
  18. template <std::int16_t value>
  19. struct x
  20. {
  21. static void f() { std::cout << value << std::endl; };
  22. };
  23.  
  24.  
  25. template <std::int16_t lowest, std::size_t ... Is>
  26. constexpr std::array<void (*)(), sizeof...(Is)>
  27. make_x_functions(index_sequence<Is...>)
  28. {
  29. return {&x<std::int16_t(lowest + Is)>::f...};
  30. }
  31.  
  32. void call_x(std::int16_t i)
  33. {
  34. constexpr std::int16_t lowest = -42;
  35. constexpr std::int16_t size = 100;
  36. constexpr auto arr = make_x_functions<lowest>(make_index_sequence<size>());
  37.  
  38. if (lowest <= i && i < lowest + size)
  39. {
  40. arr[i - lowest]();
  41. }
  42. }
  43.  
  44. int main() {
  45.  
  46. call_x(0);
  47. call_x(42);
  48.  
  49. }
  50.  
Success #stdin #stdout 0s 3312KB
stdin
Standard input is empty
stdout
0
42