fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4.  
  5. class StringProxyDemo {
  6. public:
  7. class Proxy {
  8. public:
  9. Proxy(std::string value) : _value(std::move(value)) { }
  10. std::string *operator->() { return &_value; }
  11. private:
  12. std::string _value;
  13. };
  14.  
  15. Proxy operator->() {
  16. return _function();
  17. }
  18.  
  19. StringProxyDemo(std::function<std::string()> function)
  20. : _function(std::move(function)) { }
  21.  
  22. private:
  23. std::function<std::string()> _function;
  24. };
  25.  
  26. int main() {
  27. StringProxyDemo demo(
  28. []() -> std::string {
  29. return "Hello, World!";
  30. }
  31. );
  32. std::cout << demo->size() << std::endl;
  33. std::cout << demo->c_str() << std::endl;
  34. }
  35.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
13
Hello, World!