|
From: Dima <dim...@go...> - 2010-08-12 06:14:22
|
We had this discussion before. I think the agreement was to move to
boost optional and discourage
the usage of the Null class without removing it. One could use
boost::optional it in the following way
void testOptional(boost::optional<double> x=boost::optional<double>());
void testOptional(boost::optional<double> x){
if(!x){
std::cout << "x empty" << std::endl;
}
else{
std::cout << x << std::endl;
}
}
and call it with
testOptional();
testOptional(1.2);
Kakhkhor Abdijalilov schrieb:
> Null class is rather confusing. We have Null<int>, Null<long>,
> Null<long long>, Null<unsigned long>, Null<unsigned int>, Null<float>,
> Null<double>, Null<long double> etc. Null<Size> is bound to conflict
> with some of those Nulls on 32 bit platforms, that is why a macro
> switch was needed. But the same macro brakes Null on 64 bit platforms.
> Boost::optional is much better and already used in QuantLib. We should
> use it instead of Null whenever possible. But working Null is still
> needed for backward compatibility.
>
> Only POD types need Null. Null<std::vector<double> > isn't a
> minefield. We can't accidentally do something like this
>
> POD_type x = Null<std::vector<double> >.
>
> It won't compile.
>
>
> Below is the null.hpp I am using with QuantLib. I doesn't depend on
> x64 macro and works on both 32 and 64 bit platforms.
>
> //---------------------------------------------------------------------------------------------
> #ifndef quantlib_null_hpp
> #define quantlib_null_hpp
>
> #include <ql/types.hpp>
> #include <limits>
> #include <boost/type_traits.hpp>
>
> namespace QuantLib {
>
> template <bool>
> struct FloatingPointNull;
>
> // null for floating poit types
> template <>
> struct FloatingPointNull<true> {
> static float nullValue() {
> return std::numeric_limits<float>::max();
> }
> };
>
> // null for integer types
> template <>
> struct FloatingPointNull<false> {
> static int nullValue() {
> return std::numeric_limits<int>::max();
> }
> };
>
> template <typename T>
> class Null {
> public:
> Null() {}
> operator T() const {
> return
> T(FloatingPointNull<boost::is_floating_point<T>::value>::nullValue());
> }
> };
>
> }
>
> #endif
> //---------------------------------------------------------------------------------------------
>
> Regards,
> Kakhkhor Abdijalilov.
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by
>
> Make an app they can't live without
> Enter the BlackBerry Developer Challenge
> http://p.sf.net/sfu/RIM-dev2dev
> _______________________________________________
> QuantLib-dev mailing list
> Qua...@li...
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev
>
>
|