From 8f87a035880e988307f023af5d54139b07416a9d Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Mon, 1 Jan 2018 17:37:38 -0500 Subject: [PATCH] ES.87 #1032 comment and example added. To consider: should integer comparison be broken out as its own rule? --- CppCoreGuidelines.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 296bcff..4f71d8c 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -12430,6 +12430,23 @@ For example: This invokes `istream`'s `operator bool()`. +##### Note + +Explicit comparison of an integer to `0` is in general not redundant. +The reason is that (as opposed to pointers and Booleans) an integer often have more than two resonable values. +Furthermore `0` (zero) is often used to indicate success. +Consequently, it is best to be specific about the comparison. + + void f(int i) + { + if (i) // suspect + // ... + if (i==success) // possibly better + // ... + } + +Always remember that an integer can have more that two values. + ##### Example, bad It has been noted that @@ -12442,7 +12459,7 @@ Being verbose and writing if(strcmp(p1, p2) != 0) { ... } // are the two C-style strings equal? (mistake!) -would not save you. +would not in itself save you. ##### Note