Angus Leeming <[EMAIL PROTECTED]> writes: | Lars Gullik Bjønnes wrote: >> I even think boost::optional should be used for the Cached vars >> type, then you can easily know if the cached var is set or not. >> >> template <typename T> >> class CachedVar { >> public: >> CachedVar(CachedVar const &) { var_.reset(); } >> void operator=(CachedVar const &) { var_.reset(); } >> >> CachedVar() {} >> void operator=(T const & var) { var_.reset(var); } >> operator T() const { return *var_; } >> private: >> boost::optional<T> var_; >> }; > | I'm not so sure. Apart from the fact that I don't know what the result | of '*var_' is when the variable is not set, I think it is always true | that 'T()' is a perfectly valid result here. See how we use cached | variables.
CachedVar<int> cache_; Really non-usable, but has a value that should not be used. Perfect use of optional. I am very sure. T() is not a valid result if it was not set by values that should actually be cached... So it is also a safety measure. -- Lgb