|
Boost : |
Subject: Re: [boost] [new Warnings policy] MS C4180 on the Maintenance Guidelines
From: Patrick Horgan (phorgan1_at_[hidden])
Date: 2009-11-17 14:58:35
Paul A. Bristow wrote:
-----Original Message-----
From: [1]boost-bounces_at_[hidden] [[2]mailto:boost-bounces_at_[hidden]
] On
Behalf Of
Mateusz Loskot
Sent: Tuesday, November 17, 2009 3:36 PM
To: [3]boost_at_[hidden]
Subject: Re: [boost] [new Warnings policy] MS C4180 on the Maintenance
Guidelines
Gottlob Frege wrote:
On Mon, Nov 16, 2009 at 4:08 PM, Patrick Horgan [4]<phorgan1_at_[hidden]> wrote:
C4800 Might suggest that they use a bool valued expression in the first
place, i.e. instead of foo, foo!=0, or do a static cast to bool. This is
at
times indicative of real bugs, when people turn out to not be doing what
they thought they were doing. Apparently this is one of my favorite bugs
(by favorite I don't mean that I like it either!)
C4800: int' : forcing value to bool 'true' or 'false'
I'm a big fan of using !! to convert to bool:
bool has_item()
{
Foo * foo = find_item();
return !!foo; // convert ptr to bool
}
Is that too subtle for others? It wouldn't be too subtle if it became
a common idiom. :-)
It looks like a quite recommended idiom (double-bang trick)
[5]http://www.artima.com/cppsource/safebool.html
Looks OK to me but anyone 'deprecating' this before I add it to the guidelines?
For signed integral types, using !!val folds all the negative and positive
non-zero values into true. It's the equivalent of val!=0. Sometimes,
people really mean to test for val!=0, sometimes they really mean val>0.
!!val will still be true even if val is -42. Why not say what you mean?
It's communicates better and leads to less subtle bugs. Use one of val!=0,
val>0, or whatever test you really mean to do. I understand !!val is the
same as val!=0 when I read it and hope that the original programmer also
understood that. When I maintain someone else's code, and see val!=0 I feel
more happy fuzzies that they said what they meant.
Patrick
References
1. mailto:boost-bounces_at_[hidden]
2. mailto:boost-bounces_at_[hidden]
3. mailto:boost_at_[hidden]
4. mailto:phorgan1_at_[hidden]
5. http://www.artima.com/cppsource/safebool.html
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk