Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-06-07 20:05:38


Hello all,
        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 it's usefulness extends beyond 'function'. The code is just:

namespace boost {
  struct nil_t
  {
    template<typename T> operator T*() const { return 0; }
    template<typename T, typename U> operator T U::*() const { return 0; }
  };

  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; }

  namespace {
    nil_t nil;
  }
}

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.

        Doug


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