fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct mylist {
  6. vector<int> oops { 3,5, 7, 9};
  7. int& operator[] (size_t i) {
  8. cout << "indexing overload"<<endl;
  9. return oops[i];
  10. }
  11. mylist operator[] (pair<int, int>p) {
  12. cout << "slicing overload from "<<p.first<<" to "<<p.second<<endl;
  13. return mylist(); // just for proof of concept
  14. }
  15. };
  16.  
  17. int main() {
  18. mylist l;
  19. cout<< l[2] <<endl;
  20. l[make_pair(3,5)];
  21. l[{4,8}];
  22. return 0;
  23. }
Success #stdin #stdout 0s 5648KB
stdin
Standard input is empty
stdout
indexing overload
7
slicing overload from 3 to 5
slicing overload from 4 to 8