Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-06-08 13:25:46


on 6/7/01 9:09 PM, Douglas Gregor at gregod_at_[hidden] wrote:

> The function library currently under review contains a type 'nil_t' with an
> instance 'nil'. It is a replacement for "0" or "NULL" in any code. The
> implementation is trivial, but it should probably be placed in its own header
> because its usefulness extends beyond 'function'. The code is just:

Like someone else said, we would have to avoid the name "nil" since it's
used as a macro in Apple system code. I like the name "null" better. Also,
shouldn't there be comparisons with pointers-to-member too? A revised class
could be:

namespace boost
{

struct null_t
{
    template < typename T > operator T*() const { return 0; }
    template < typename T, class U > operator T U::*() const { return 0; }

}; // boost::null_t

template<typename T>
    inline bool operator==(const T* p, const nil_t&) { return !p; }
template<typename T>
    inline bool operator==(const nil_t&, const T* p) { return !p; }
template<typename T>
    inline bool operator!=(const T* p, const nil_t&) { return p; }
template<typename T>
    inline bool operator!=(const nil_t&, const T* p) { return p; }
template<typename T, class U>
    inline bool operator==(T U::* const p, const nil_t&) { return !p; }
template<typename T, class U>
    inline bool operator==(const nil_t&, T U::* const p) { return !p; }
template<typename T, class U>
    inline bool operator!=(T U::* const p, const nil_t&) { return p; }
template<typename T, class U>
    inline bool operator!=(const nil_t&, T U::* const p) { return p; }

} // boost

namespace
{
    boost::null_t null;
}

It could go into <boost/utility.hpp>.

A problem is that we would have to add a BOOST_NO_MEMBER_TEMPLATE_OPERATORS
to <boost/config.hpp> and define it for Metrowerks CodeWarrior (both 5.3 and
6.2, I think).

> Advantages of 'nil' over NULL:
> - NULL is not necessarily portable. Some C++ compilers pick up bad
> definitions of NULL from the system C libraries, making it useless.
> - The type of NULL is unspecified, so it is impossible to overload a
> function based on it.
>
> Advantages of 'nil' over 0:
> - 0 is an integer. It converts to pointer types but doesn't convey much
> information to the user.
> - the type of 0 is "const int". Overloading a function to take a null value
> based on const int also allows any integer value to be passed in, and the
> error cannot be detected at compile-time.

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk