[Development] 0 vs. NULL
René J.V. Bertin
rjvbertin at gmail.com
Thu Oct 8 13:01:50 CEST 2015
On Thursday October 08 2015 09:52:14 Welbourne Edward wrote:
> > A bit of a generic question, [...] about the preferred use of 0 instead of NULL.
>
> One of the habits of C++ that a C programmer always finds weird.
Yeah, especially when they're used to being annoyed about all those "redundant" casts they have to put everywhere ;)
> That makes sense - in the ... of a var-args parameter-list, the compiler
> has no clue what it's passing, so it uses the type of the expression
> passed; and it'll read 0 as the int, which is likely 32-bit. If the
Hence the forehead-slapping aspect ...
> and I'm not entirely sure about NULL, for that matter (it might just
> expand to 0, at least in C, but perhaps not in C++).
Given how NULL is usally defined as ((void*)0) or equivalent, there should not be any issues in that aspect.
> Well, it's nice to hear we don't use var-args. The other pit-fall
Wait, I didn't claim that. Or should I read it's nice for me to hear that this is in fact the case?
Is it, btw? qDebug() and family have forms that takes a format string with arguments, no? I could just go look at the source code...
> that's easy to run into is overloading-by-signature: passing 0 might not
> get you the overload that has a pointer as that parameter, unless you
> expressly static_cast<>(0) to the type of the intended parameter.
I think I've already run into situations where the compiler protected me from that situation by detecting the ambiguity, but I'm not sure one can count on that in all possible combinations.
R.
More information about the Development
mailing list