fork download
  1.  
  2. #include <iostream>
  3.  
  4. class Foo
  5. {
  6. public:
  7. Foo() : is{1,2,3,4,5,6,7,8,9,10},ds{1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1} {}
  8.  
  9.  
  10. template <typename T> struct type_c{};
  11. template <size_t I> struct int_c{};
  12.  
  13. template<typename T,size_t I>
  14. auto get()
  15. { return get_impl(type_c<T>(),int_c<I>()); }
  16. private:
  17.  
  18. template <size_t I>
  19. auto get_impl(type_c<int>,int_c<I>)
  20. { return is[I]; }
  21.  
  22. template <size_t I>
  23. auto get_impl(type_c<double>,int_c<I>)
  24. { return ds[I]; }
  25.  
  26.  
  27. int is[10];
  28. double ds[10];
  29. };
  30. int main() {
  31. Foo foo;
  32.  
  33. int i = foo.get<int,0>();
  34. double d = foo.get<double,2>();
  35. std::cout << i << " " << d << std::endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 4532KB
stdin
Standard input is empty
stdout
1 3.3