Boost logo

Boost :

Subject: Re: [boost] [signals2] Test failure in C++11 (trivial fix for incorrect usage of boost::optional)
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-03-05 19:02:30


On 1/03/2014 05:26, Quoth Peter Dimov:
> This is obviously a matter of stylistic preference, but still, mine runs
> contrary to yours. x == false is an anti-pattern and should never be
> preferred to !x, even when x is bool; similarly, x == true is an
> anti-pattern and should never be preferred to plain x. Boolean logic
> doesn't have ==. Nobody prefers ( x == y ) == false to !( x == y ) or x
> != y, let alone ( x == y ) == true to x == y.

FWIW, I've seen official coding styles to explicitly use "x == false"
instead of "!x", primarily because it's easier to miss the "!" either
when writing or later reviewing the code. At least for "real" bools,
the compiler will produce the same code either way; it can get a little
more troublesome with overloaded operators (which is of course what
prompted this discussion in the first place).

"x == true" is banned except when dealing with optional<bool>; for that
simply using "x" is preferred, with a fallback to "x != false" where
required or if it makes it easier to read.

(Where it comes up most often is in using (x != FALSE) where x is a BOOL
in the Windows sense, not actually a bool. This avoids a compiler warning.)

TLDR: I don't think I agree that "!x" is always preferred over "x ==
false". (And in the case of optional<bool>, it changes behaviour.)


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk