|
From: Luigi B. <lui...@gm...> - 2007-05-18 14:48:06
|
On Fri, 2007-05-18 at 15:00 +0200, DU VIGNAUD DE VILLEFORT FRANCOIS
GASAPRD PHI wrote:
> >As for QL_NULL_REAL, it doesn't really matter what value is used as long as >it's a determined one
>
> Since it is now used as some kind of unitialized value by the quotes classes, I would say that it is no longer such an immaterial detail.
Sure. We picked a value which is unlikely to be chosen as a valid one;
but since it has to be a value anyway (we could use NaN, but it's not
very portable) it's not foolproof.
> Maybe you have thought about boost::optional for the same reason. ;-)
> It would be cleaner indeed. However I'm not convinced that it's a good solution yet.
> IMO boost::optional a pefect solution in the case of a free function which can return invalid result even in normal situations. (like the second example of boost optional tutorial).
>
> On the hand it seems to me that using it for a function member would add some spurious trickyness to the class. What do you think of this implementation ?
>
> Quote():isValid_(false){}
>
> virtual void resetQuote() {
> isValid_ = false;
> }
>
> virtual bool isValid() const {
> return isValid_;
> };
> private:
> bool isValid_;
Is this the base class? In this case, isValid_ should be protected and
derived classes would have to manage it. Using optional wouldn't be so
tricky: we could just write SimpleQuote as (without namespace boost)
class SimpleQuote {
private:
optional<Real> value_;
public:
explicit SimpleQuote(optional<Real> value = none)
: value_(value) {}
bool isValid() const {
return value_; // automatic conversion to bool
}
Real value() const {
return *value_; // access the actual Real
}
Real setValue(Real x) {
value_ = x; automatic conversion to optional
... // notify etc.
}
};
SimpleQuote q1; // not initialized, isValid() returns false
SimpleQuote q2(42.0); // double automatically converted to optional
Later,
Luigi
----------------------------------------
The First Rule of Optimization: Don't do it.
The Second Rule of Optimization (For experts only): Don't do it yet.
-- Michael Jackson
|