Hi, LLVM. I have a question I'd like to get put into the official style guidelines: do we prefer NULL or 0 for C++ objects? I've seen both throughout the code. Personally I prefer NULL, since it establishes that something is a pointer and not an integer (or integer-constructed object, but thankfully we avoid implicit conversions in LLVM/Clang). But I think I read somewhere that 0 is more C++esque. (And C++11 nullptr's not available yet, of course.) Similarly, when testing for the null pointer, is it better to use: 1. (x) and (!x) 2. (x) and (x == NULL) // or 0 3. (x != NULL) and (x == NULL) 4. (NULL != x) and (NULL == x) 5. ...some other combination or something I haven't thought of I don't care about this one as much. Still, in this case I think the first is fairly standard, though the second does make the explicit test for NULL stand out a little more (fewer copy/paste errors, perhaps?). IMO (3) and (4) are overkill. Thoughts? Jordy
On Wed, Aug 17, 2011 at 4:41 PM, Jordy Rose <jediknil at belkadan.com> wrote:> But I think I read somewhere that 0 is more C++esque.I believe Stroustrup espoused this at one point (perhaps even on his website) on the basis that using NULL gives you a false sense of security - which isn't entirely true now that compilers (GCC & clang presumably) will warn you about using NULL in non-pointer contexts.> (And C++11 nullptr's not available yet, of course.)My 2c is that this is a good reason to use NULL over 0 - it'll be way easier to s/NULL/nullptr/ when upgrading to C++11 in the future than it'll be to hunt down all those 0s used as pointers. Though I suppose we can always enhance the compiler to warn about 0 used in pointer contexts in C++11... Hmm, we could do that today - hmm, is that something we should do? I think it'd be rather neat/helpful - it could have a fixup & all that.> 1. (x) and (!x)This is my usual approach/preference. Then you can also do things like: if (T *x = foo()) ...
> using NULL gives you a false sense of security - which isn't entirely > true now that compilers (GCC & clang > presumably) will warn you about using NULL in non-pointer contexts.Hmm, nope, can't seem to find that warning in clang - gcc gives it though: foo.cpp:4:11: warning: converting to non-pointer type "int" from NULL
On Aug 17, 2011, at 4:41 PM, Jordy Rose wrote:> Hi, LLVM. I have a question I'd like to get put into the official style guidelines: do we prefer NULL or 0 for C++ objects? I've seen both throughout the code. > > Personally I prefer NULL, since it establishes that something is a pointer and not an integer (or integer-constructed object, but thankfully we avoid implicit conversions in LLVM/Clang). But I think I read somewhere that 0 is more C++esque. (And C++11 nullptr's not available yet, of course.) > > > Similarly, when testing for the null pointer, is it better to use: > 1. (x) and (!x) > 2. (x) and (x == NULL) // or 0 > 3. (x != NULL) and (x == NULL) > 4. (NULL != x) and (NULL == x) > 5. ...some other combination or something I haven't thought of > > I don't care about this one as much. Still, in this case I think the first is fairly standard, though the second does make the explicit test for NULL stand out a little more (fewer copy/paste errors, perhaps?). IMO (3) and (4) are overkill.I use #4 most of the time. Rationale: * Make it explicit * Constant on the left hand side. -- Marshall Marshall Clow Idio Software <mailto:mclow.lists at gmail.com> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
On Aug 17, 2011, at 9:06 PM, Marshall Clow wrote:> On Aug 17, 2011, at 4:41 PM, Jordy Rose wrote: > >> Hi, LLVM. I have a question I'd like to get put into the official style guidelines: do we prefer NULL or 0 for C++ objects? I've seen both throughout the code. >> >> Personally I prefer NULL, since it establishes that something is a pointer and not an integer (or integer-constructed object, but thankfully we avoid implicit conversions in LLVM/Clang). But I think I read somewhere that 0 is more C++esque. (And C++11 nullptr's not available yet, of course.) >> >> >> Similarly, when testing for the null pointer, is it better to use: >> 1. (x) and (!x) >> 2. (x) and (x == NULL) // or 0 >> 3. (x != NULL) and (x == NULL) >> 4. (NULL != x) and (NULL == x) >> 5. ...some other combination or something I haven't thought of >> >> I don't care about this one as much. Still, in this case I think the first is fairly standard, though the second does make the explicit test for NULL stand out a little more (fewer copy/paste errors, perhaps?). IMO (3) and (4) are overkill. > > > I use #4 most of the time. > Rationale: > * Make it explicit > * Constant on the left hand side.Please don't do this in LLVM code. Unless you're the terminator of a varargs call, I'd prefer to use 0 instead of NULL, and definitely constants on right hand sides of comparisons. -Chris