fork(1) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct smth
  6. {
  7. template <int i> struct key_t {};
  8.  
  9. int x; int get(key_t<1>) { return this->x; }
  10. double y; double get(key_t<2>) { return this->y; }
  11.  
  12. template <int i> auto get() -> decltype (this->get(key_t<i>())) { return this->get(key_t<i>()); }
  13. };
  14.  
  15. int main()
  16. {
  17. smth s = {1, 2.5};
  18.  
  19. cout << s.x << ' ' << s.y << endl;
  20. cout << s.get<1>() << ' ' << s.get<2>() << endl;
  21.  
  22. return 0;
  23. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1 2.5
1 2.5