Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-06-08 13:45:50

On Friday 08 June 2001 10:49 am, Iain.Hanson_at_[hidden] wrote:
> gregod (gregod_at_[hidden]) wrote
> snip ...
> >Advantages of 'nil' over NULL:
> > - NULL is not necessarily portable. Some C++ compilers pick up bad
> >information to the user.
> The problem is that there is no portable definition of nil. nil has been
> defined by a bzillion different people to mean different things.
> Stroustrup's D & E discusses the problems with this. CORBA has a nil,
> Czarnecki & Eisenecker have a nil in the software from their book
> 'Generative Programming' and many other class libraries have a nil. This
> seems like a bad idea IMHO.

In my opinion, Bjarne doesn't make a strong case that no portable definition
of nil exists. He mentions only the C definition:
#define nil (void*)0

Indeed, he notes that there is a problem with using 0:

void f(char*);
void g() { f(0); }

If an overload of "f" is added:
void f(int);

the behavior of "g" has been changed by adding the overload, because now the
"int" overload is chosen.

The definition of nil under consideration won't have such a problem - it can
be converted to null pointers only.

> >Advantages of 'nil' over 0:
> > - 0 is an integer. It converts to pointer types but doesn't convey
> > much information to the user.
> Initially, when I was converting to C++ from C, I wanted to use NULL,NIL,
> ZERO or any symbolic name. Once I read D & E I got very comfortable with
> the humble 0. It coveys more than enough information for me.
> > - the type of 0 is "const int". Overloading a function to take a
> > null va lue
> >based on const int also allows any integer value to be passed in, and the
> >error cannot be detected at compile-time.
> Personally, I don't see much ( if any ) need to be able to overload
> specifically on a null pointer over any other pointer.
> /ikh

The nil_t type only exists because I found that I needed it for
boost::function. Essentially, I have the following:

class Foo {
  template<typename T> Foo& operator=(T);

So an object of type "Foo" can be assigned anything. Now, I'd like to be able
to clear out the object. A possibility:

Foo f;
f = 'a';
f = std::string("Foo");

I'd also like to be able to set the object to some null value (i.e., clear
out whatever value it has stored):
f = 0;

However, this doesn't work at all. If I add an operator= overload such as:
Foo& operator=(const void*);

It will never be chosen because "0" has type "const int". So, I need a
special type, nil_t, and "nil" is just an object of type nil_t:
Foo& operator=(const nil_t&);
// ...
f = boost::nil;


Boost list run by bdawes at, gregod at, cpdaniel at, john at