Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. template<class T>
  2. class Susp
  3. {
  4. // thunk
  5. static T const & thunkForce(Susp * susp) {
  6. return susp->setMemo();
  7. }
  8. // thunk
  9. static T const & thunkGet(Susp * susp) {
  10. return susp->getMemo();
  11. }
  12. T const & getMemo() {
  13. return _memo;
  14. }
  15. T const & setMemo() {
  16. _memo = _f();
  17. _thunk = &thunkGet;
  18. return getMemo();
  19. }
  20. public:
  21. explicit Susp(std::function<T()> f)
  22. : _f(f), _thunk(&thunkForce), _memo(T())
  23. {}
  24. T const & get() {
  25. return _thunk(this);
  26. }
  27. private:
  28. T const & (*_thunk)(Susp *);
  29. mutable T _memo;
  30.  
  31. std::function<T()> _f;
  32. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement