fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <functional>
  4.  
  5. using namespace std;
  6.  
  7. class A {
  8. public:
  9. std::function<int()> operator[] (int i) {
  10. return [i](){
  11. return i;
  12. };
  13. }
  14. };
  15.  
  16. int main() {
  17. A a;
  18. cout << a[5]() << endl; // <- 5
  19. cout << a[6]() << endl; // <- 6
  20.  
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
5
6