|
From: Dima <dim...@go...> - 2009-09-18 14:05:33
|
I had a closer look at it and would like to propose some changes. The first
thing
that I think is worth discussing is the comparison against
std::numeric_limits<T>::max.
So the following code:
Real myReal=(std::numeric_limits<float>::max)();
bool realIsNull=(myReal==Null<Real>());
would set the boolean to true. But myReal is actually a Real number, the
largest one
which can be represented on the system. I agree that this will work most of
the time,
but I think it would make more sense to compare it against
std::numeric_limits<float>::quiet_isNaN
Comparing any real against this object type will return false, exactly what
we want. The second
thing that I'd like to mention is that for the class
class Foo{
private:
Real var_;
public:
Foo():var_(10.0){}
bool operator==(const Foo& rhs) {
return var_==rhs.var_;
}
};
which has a standard constructor, the following will yield a boolean which
is true
Foo myFoo;
bool myFooIsNull=(myFoo==Null<Foo>());
In this case, we are comparing against a standard constructor, which is not
really a Null object,
since some member variables are initialized. I propose something similar too
#include<limits>
#include <boost/static_assert.hpp>
template <class Type> class NullNew {
public:
NullNew() {}
operator Type() const {
BOOST_STATIC_ASSERT(std::numeric_limits<Type>::has_quiet_NaN);
return std::numeric_limits<Type>::quiet_NaN();
}
bool operator==(const NullNew<Type>&) {
return true;
}
};
The following happens here: first of all, we don't need a template
definition for all types
like int, double, long int.... Second: we are checking against quiet_NaN.
Third: I disable
any casting of anything which doesn't have the quiet_NaN implementation.
Boost static
assert lets the compilation fail in this case. I don't know whether we are
using Null<Foo>
for any other objects than Real, Natural.. In case we do, this will not
work. Until we have
something better, e.g. a isNull implementation for all objects (which could
then be incorporated
easily in the class above), we should prevent the cast operator from
returning a standard
instance.
What do you think?
|