我已经开始阅读有关懒惰评估的博客。这是博客的链接 https://bartoszmilewski.com/2014/04/21/getting-lazy-with-c/
我不明白为什么这段代码中需要中间的“ thunk”方法:
template<class T>
class Susp
{
// thunk
static T const & thunkForce(Susp * susp) {
return susp->setMemo();
}
// thunk
static T const & thunkGet(Susp * susp) {
return susp->getMemo();
}
T const & getMemo() {
return _memo;
}
T const & setMemo() {
_memo = _f();
_thunk = &thunkGet;
return getMemo();
}
public:
explicit Susp(std::function<T()> f)
: _f(f), _thunk(&thunkForce), _memo(T())
{}
T const & get() {
return _thunk(this);
}
private:
T const & (*_thunk)(Susp *);
mutable T _memo;
std::function<T()> _f;
};
如果没有它们,可以将代码重构为以相同的方式工作。那么,这背后的想法是什么?