3 ms·
> But does C need a nullptr keyword? Yes, it does. > If you're programming in C, you usually define 0 as an invalid value, or a null value. That was also the
by roqi 3y ago
> But does C need a nullptr keyword?
Yes, it does.
> If you're programming in C, you usually define 0 as an invalid value, or a null value.
That was also the usual pattern in C++ when there was no alternative. Once nullptr was introduced in C++, NULL or 0 quickly became a code smell.
> C doesn't have the insane type system C++ has and doesn't have a very strong need to make a distinction between a pointer or an integer, since they're all in the end numbers.
C++'s type system is far from insane. It's actually one of it's killer features.
You're both entirely oblivious to the need to not conflate pointers with integers and failing to present any case in favour of the legacy and broken use of NULL, and in the process failing to address all family of known error patterns involving it.
> The printf example you gave is an example of garbage in, garbage out. If NULL is a macro not defined as a pointer sized integer, then you're at fault here.
Again, you seem to be completely oblivious to the problem domain. NULL is not a macro as far as C or C++ compilers are concerned. NULL is a magic constant that's resolved at preprocessing time. Replacing NULL with nullptr means a magic constant is replaced by a concrete type, and thus whole family of errors can be avoided with compile time checks. Claiming that the developers who wrote in bugs are at fault for inadvertently adding bugs makes no sense at all because it does not solve any problem at all, and instead is just cynical finger pointing. I take compile-time checks over unhelpful finger pointing all day every day.
- colonwqbang 3y agoNULL is a macro. The original mistake by the standards committee was allowing implicit conversions from integer to pointer. I.e. allowing NULL to be defined as simply 0. If NULL had been defined always as ((void *) 0) then I don't see that we would have had a problem. But that's all history now and in this situation I can see that adding nullptr becomes a reasonable way out. It's ironic though that the fix for the different ways to write null is to add yet another way.
- roqi 3y ago> NULL is a macro. You're missing the whole point. As per the C standard, NULL is an implementation-defined null Pointer constant. Macros are resolved in the preprocessing step. The compiler does not know what a macro is. What the compiler knows is whatever the preprocessor passes off in place of the macro. This means the compiler only sees a constant, and has no way to tell what that constant means. If instead of passing random pointer constants you pass an actual type, now the compiler can tell more things. > If NULL had been (...) Irrelevant. The whole point is that it wasn't the committee looked at the problem, and it determined that using a dedicated type is safer, more powerful, and more elegant than passing magic numbers around.
- ori_b 3y agoI am on the committee. As you can probably tell by my posts, this was not unanimous. I'd categorize nullptr as 'mostly harmless'