4

I'm reading A Tour of C++ by Bjarne Stroustrup to brush up on my C++.

In Chapter 1, section 1.8, Stroustrup writes:

The most common case is testing a variable against 0 (or the nullptr). To do that, simply leave out the explicit mention of the condition. For example:

void do_something(vector<int>& v)
{
    if (auto n = v.size()) {
        // ... we get here if n!=0 ...
    }
    // ...
}

In my own code, I always try to be explicit, and to accurately convey my intention. I believe that this is considered a general programming best practice.

I consider writing elaborate expressions that evaluate to logical true or false (rather than to 0 or nullptr) as part of this approach. It can also help to avoid subtle implicit casting bugs. In fact, I would think that modern C++ allowed this kind of implicit conversion only for backward compatibility purposes, and would actually expect it to be discouraged in new code.

At the same time, I believe that in his book, Stroustrup suggests best practices that are generally accepted by the community.

My questions are:

  1. Is this style of implicit conversion really considered a best practice in the C++ development community?
  2. If so, doesn't it contradict other best practices and the general underlying goals of strong typing?
0

2 Answers 2

4
  1. Is this style of implicit conversion really considered a best practice in the C++ development community?

There is no such thing as "the C++ development community", and developers do not unanimously agree to one canonical set of best practices.

That said yes, implicit conversion to bool really is conventionally used in if statements. And there undoubtedly are some who prefer not to rely on it.

  1. If so, doesn't it contradict other best practices and the general underlying goals of strong typing?

Depends on what those other best practices and general underlying goals try to achieve. Given the wide ranging diversity of opinions, it is quite possible for one best practice to contradict another.

In general, C++ has many implicit conversions between types, and programmers can even have user defined conversions for classes. The fact that the language has implicit conversions implies that there are at least some who consider that implicit conversions are acceptable.

Opinions on which implicit conversions are OK (and in which contexts) differ. Usually, the arguments boil down to how the program could be misinterpreted, and how much a redundantly explicit conversion would unnecessarily draw attention away from the relevant and important parts of the code.

Implicit conversions are just one choice in balancing the dimensions of verbosity versus terseness and explicit versus implicit.

3

This is of course an opinion question and as such is probably off-topic for SO but I'll give a go at answering anyway.

I prefer the implicit test both because it is easier to write and also because it is easier to read, the intention of if(ptr) or if(!ptr) is immediately obvious (the intention of if(nullptr != ptr) is just as obvious but does take a slight bit more time to mentally parse).

Also, just like how I prefer if(1 == value) over if(value == 1) there is less chance to mess something up with the implicit test. (It would be easy to write if(ptr = nullptr) by accident).

2
  • I agree that if(ptr) is more readable, and for pointers - I think it makes sense to use. But imo if(some.size()) is harder to comprehend than if(some.size() > 0)... by the way, the fact that if(value = 1) is even allowed, and "forces" us to write the less-natural-to-read if(1 == value) is a consequence of the implicit conversion being allowed (for variables that are not pointers) to begin with...
    – obe
    Apr 10, 2020 at 20:48
  • compiler might warn for if(value = 1), and requires extra parenthesis when intentional: if ((value = 1)). I prefer if(!some.empty()) over version with size :) !ptr doesn't do (implicit) conversion. I already people doing if (!!ptr) to avoid implicit conversion. I found it more confusing though.
    – Jarod42
    Apr 10, 2020 at 21:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.