My understanding - and I'm sure I'm about to learn - is that nullptr
was added to C++ to formalise the convention that a zero value for a pointer means the pointer does not point to a valid object. Is there a case for (or against) adding a corresponding nullchar
to formalise the convention that a zero value for a char means the char is not a valid character but is instead the terminator of a string? I can see a few things which the two cases have in common:
Type safety: 0
can have several different types, so it's easy to mistakenly assign a non-pointer variable to zero instead of the pointer variable you meant to. It's also easy to call the wrong overloaded function if you get it just right. Having a special and strongly-typed value prevents this kind of mistake, and this could be true for char
types as well.
Expressiveness: Assigning a variable to be nullptr
makes it clear to the reader that the variable is a pointer (because only a pointer can be assigned to that value) and that it points to no object. Assigning a char to nullchar
would add the same kind of clarity and readability.
There's more, but I'm sure you get the idea. So why do we have nullptr
but not nullchar
?