J
Joshua Maurice
Ahh, probably mentions that it must be a compile-time expression of
int 0. I'm sorry. I'm entirely wrong. That's still horribly broken as
language design.
Any value that evaluates to zero and is an integral constant (int, char,
bool etc.) converts to a null pointer. You can even do:
void* ptr = true - true;
...because true-true equals false.
That's why it is good style to use a symbolic constant as null pointer
(NULL or better yet nullptr) to document that you're assigning a pointer
value and not an int.
A const variable initialized with an integral constant is also an
integral constant; for example, you can use it as size of an array:
const int size = 42;
char array[size];
I see the type safety value for allowing integral constant expressions
in array sizes like your code example, but I strongly disagree with
the value in allowing your other example of
void* ptr = true - true;
Allowing this is \less\ type safety. Surely there's some more sensible
language rules than "integral constant expressions of 0"? And yes, I
suppose the new nullptr is one such way.
Ex:
int const x = 0;
int * y = x;
This nonsense does not compile in C89, but apparently it's legal C+
+03. (At least according to Comeau.) I call that bad language design
in C++. What's the rule for C? That an int literal 0 is implicitly
convertible to the null pointer? Why did C++ change that?