Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/.cpp Secret

Last active November 22, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/cf4cbf3e7b2cfc322716 to your computer and use it in GitHub Desktop.
Save rightfold/cf4cbf3e7b2cfc322716 to your computer and use it in GitHub Desktop.
template<typename F>
class lazy {
public:
explicit lazy(F f) : f(std::move(f)) { }
auto const& force() const {
if (!x) {
x.reset(f());
}
return *x;
}
private:
F f;
mutable boost::optional<decltype(f())> x;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment