fork download
  1. #include <iostream>
  2. struct Foo{
  3. int value = 0;
  4. int& operator[](size_t i) {
  5. std::cout << "non-const\n";
  6. return value;
  7. }
  8.  
  9. const int& operator[](size_t i) const {
  10. std::cout << "const\n";
  11. return const_cast<Foo*>(this)->operator[](i);
  12. }
  13. };
  14.  
  15. int main(){
  16. const Foo g;
  17. //g[0] = 3;
  18. Foo f;
  19. f[0];
  20. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
non-const