I have to keep one double value cached. After it is used, it should be invalidated. Two alternatives
One is to add boolean flag, true
when cached value is good, when it is used set it to false
, and when flag is false, recalculate and refill.
Second one is more interesting - I could keep it as double value and use NaN as invalid/need to recalc flag.
double get() const {
if (!isnan(_value)) {
double t = _value;
_value = std::numeric_limits<double>::quiet_NaN;
return t;
}
}
Any objections against it? Any thoughts on efficiency?