0

Are there any coding guidelines on what comparison, either:

if (ptr != nullptr) ...

or

if (ptr) ...

is better in C++(11)?

This question is not duplicate of C: Comparison to NULL, in the latter it is said that these forms are equivivalent in C.

Neither this question is opition based: I'm not asking of what do you prefer, but whether there are any coding standards or guidelines, which tell to prefer one form or another.

Community
  • 1
  • 1
Mikhail
  • 20,685
  • 7
  • 70
  • 146
  • 1
    So you're asking for the opinions of code guideline writers instead of ours? – R. Martinho Fernandes Sep 09 '13 at 08:24
  • Well, actually, yes :) But I think this is a better choice, since guidelines are tools to formalize opinions. – Mikhail Sep 09 '13 at 08:26
  • 1
    Or more accurately, guidelines *are* formalized opinions. =P – WhozCraig Sep 09 '13 at 08:29
  • 1
    There are no official, universal C++ coding standards, so your question cannot be answered. It could be answered for [Sutter/Alexandrescu](http://www.gotw.ca/publications/c++cs.htm), for [GCC](http://gcc.gnu.org/wiki/CppConventions), for [Root](http://root.cern.ch/drupal/content/c-coding-conventions), for [Geosoft](http://geosoft.no/development/cppstyle.html), for [Google](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml)... but not universally. – Daniel Daranas Sep 09 '13 at 08:29
  • 1
    If you're asking for _guidelines,_ then it _is_ opinion-based :-) That's what guideline means, a guide rather than a flat-out rule. Even coding "standards" are really guidelines in the context of the ISO standard, which allows you to write your code however you wish, provided it complies. – paxdiablo Sep 09 '13 at 08:29
  • 2
    Except whose guidelines to use is a matter of opinion... – Yuushi Sep 09 '13 at 08:29
  • 1
    The two popular standards or guidelines are: if you work alone - pick the one you like and stick to it; if you're working in a team - follow your colleagues. – molbdnilo Sep 09 '13 at 08:29
  • Barring a functional difference between the two (and I can't see how there would be once optimized), there is no "better"; there is only "I like this more.", So I'm not sure this can be answered. – WhozCraig Sep 09 '13 at 08:35
  • @WhozCraig Even before optimization. `if` requires a `bool`. The conversion of a pointer to `bool` is defined as comparing it to `0` (or any other null pointer constant). Given `if ( ptr )`, the first thing a compiler will do is convert it into `if ( ptr != 0 )`. – James Kanze Sep 09 '13 at 08:43

2 Answers2

2

Every coding guideline I've seen insists on

if ( ptr != nullptr )

or

if ( ptr != NULL )

in pre-C++11, which is the usual case. Using 0 instead of NULL is also widespread, and the choice could be considered a question of style (in the sense that there a valid arguments for both points of view).

In general, coding guidelines tend to avoid implicit conversions (with the possible exception of non-lossy conversions, like int to double), as they make the code harder to read.

On the other hand, coding guidelines can't avoid ubiquitous idioms (not the case here), so will generally accept the implicit conversion of a stream to a type which can act as a boolean. (Part of the motivation here is probably the fact that the alternatives that programmers come up with are generally wrong. Better an implicit conversion which works than code which doesn't.)

James Kanze
  • 150,581
  • 18
  • 184
  • 329
0

As i got it right in the c++11 standard nullptr isn't handled as a numeric value, so it isn't 0 as the NULL define in C is. It's more just NULL in a own way and won't be treat as the numeric 0, mostly to avoid missmatches with templates.

So it would probably be prefered asking for

if (ptr != nullptr) ... as

nullptr isn't called to be NULL in any way.

dhein
  • 6,431
  • 4
  • 42
  • 74
  • `nullptr` isn't called to be NULL in any way - what do you mean? – Mikhail Sep 09 '13 at 08:34
  • I want to say, that nullptr is equvivalent to "Nothing" in vba. What is kind of different to the NULL we know in C as far i know vba well enough. so nullptr and NULL arent the same. and nullptr isn't to be interpreted as numeric `0` as the define of NULL is. or am I wrong? – dhein Sep 09 '13 at 08:37
  • No, `nullptr` will not be treated as numeric `0`. – R. Martinho Fernandes Sep 09 '13 at 08:37
  • So why downvoting? thats exactly what I said. – dhein Sep 09 '13 at 08:38
  • Maybe you forgot a "not" somewhere, then. – R. Martinho Fernandes Sep 09 '13 at 08:38
  • @R.MartinhoFernandes The fact that `nullptr` will not be treated as numeric 0 is, in fact, the very motivation for its existence. (And the reason that `NULL` is not `nullptr` is because doing so would break existing code. Code that I'd be in favor of breaking, but I didn't make that decision.) – James Kanze Sep 09 '13 at 08:40
  • True story, I'm sorry yeah i wrote will and not won't v.v – dhein Sep 09 '13 at 08:40
  • You're right, but this is not an answer to my question. I was asking about guidelines and recommendations, not about how `nullptr` works. – Mikhail Sep 09 '13 at 08:49
  • @Mikhail it is in so far answering your question, as nullptr isn't to be a bool value in any case, so the if statement would probably be never accpeting just a value of `nullptr` and this would get corrected on compilation, so my answer was, -> its prefered to write the comparrison out as the other way would strictly argumenting be implementation defined. But this is just my opinion, and i guess the standard doesn't says so about the behavior. – dhein Sep 09 '13 at 08:52
  • What? It's not implementation defined at all. It's perfectly well-defined and has the same semantics. – R. Martinho Fernandes Sep 09 '13 at 12:30
  • @R. Martinho Fernandes I said, in my own view it could be, but the standard doesn't says so. I never said that it IS implementation defined. "But this is just my opinion, and i guess the standard doesn't says so about the behavior." – dhein Sep 09 '13 at 12:31