fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <type_traits>
  4.  
  5. using namespace std;
  6.  
  7. struct vec
  8. {
  9. double x;
  10. double y;
  11. double z;
  12. };
  13.  
  14. namespace details
  15. {
  16. template <typename T>
  17. using subscript_function = double(*)(const T&);
  18.  
  19. template <typename T>
  20. constexpr double X(const T& param) { return param.x; }
  21.  
  22. template <typename T>
  23. constexpr double Y(const T& param) { return param.y; }
  24.  
  25. template <typename T>
  26. constexpr double Z(const T& param) { return param.z; }
  27. }
  28.  
  29. template <typename T, typename = void>
  30. constexpr details::subscript_function<T> my_temp[] = { &details::X<T>, &details::Y<T> };
  31.  
  32. template <typename T>
  33. constexpr details::subscript_function<T> my_temp<T, enable_if_t<is_floating_point<decltype(details::X(T()))>::value, T>>[] = { &details::X<T>, &details::Y<T>, &details::Z<T> };
  34.  
  35.  
  36. int main() {
  37. vec foo = { 1.0, 2.0, 3.0 };
  38.  
  39. for(const auto i : my_temp<decltype(foo)>) {
  40. cout << (*i)(foo) << endl;
  41. }
  42. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
1
2