fork download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. template <class T1, class T2>
  5. class myPair {
  6. std::pair<T1, T2> member;
  7. public:
  8. myPair() = default;
  9. myPair(T1 x, T2 y) : member(std::make_pair(x, y)) {}
  10.  
  11. template <std::size_t N>
  12. const auto& operator[](std::integral_constant<std::size_t, N>) const {
  13. static_assert(N < 2, "Index out of range");
  14.  
  15. return std::get<N>(member);
  16. }
  17.  
  18. template <std::size_t N>
  19. auto& operator[](std::integral_constant<std::size_t, N>) {
  20. static_assert(N < 2, "Index out of range");
  21.  
  22. return std::get<N>(member);
  23. }
  24. };
  25.  
  26. template <std::size_t N>
  27. using uint_ = std::integral_constant<std::size_t, N>;
  28.  
  29. int main() {
  30. myPair<std::string, int> p("Hello", 42);
  31.  
  32. std::cout << p[uint_<0>{}] << " " << p[uint_<1>{}] << std::endl;
  33. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Hello 42