fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. class foo
  5. {
  6. public:
  7. void visitWith(const std::function<void(foo&)> &v) { v(*this); };
  8. void visitWith(const std::function<void(const foo&)> &v) const { v(*this); };
  9. };
  10. class bar {
  11. foo *f = new foo;
  12. public:
  13. void visitFoosWith(const std::function<void(const foo&)>& v) const {
  14. f->visitWith(v); // Visual C++ doesn't like this
  15. }
  16. };
  17.  
  18. int main()
  19. {
  20. bar b;
  21. b.visitFoosWith([&](const foo&){ std::cout << "visited a const foo!" << std::endl; });
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 4224KB
stdin
Standard input is empty
stdout
visited a const foo!