Re: Null class bug on x64 targets

Posted by Kakhkhor Abdijalilov on
URL: http://quantlib.414.s1.nabble.com/Null-class-bug-on-x64-targets-tp9091p9093.html

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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev