From 10d1d9dbc5e8a9deeeae20ae71534213bc4831f7 Mon Sep 17 00:00:00 2001 From: hsutter Date: Tue, 29 Dec 2015 10:15:43 -0800 Subject: [PATCH] Addressed PR #140. --- CppCoreGuidelines.md | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 3c8d7fe..e46ae47 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -6424,7 +6424,7 @@ Enumeration rule summary: ##### Reason -Macros do not obey scope and type rules. +Macros do not obey scope and type rules. Also, macro names are removed during preprocessing and so usually don't appear in tools like debuggers. ##### Example @@ -9326,17 +9326,31 @@ The result is undefined and probably a crash. ##### Note -this also applies to `%`. +This also applies to `%`. -##### Example +##### Example; bad - ??? + double divide(int a, int b) { + return a/b; // BAD, should be checked (e.g., in a precondition) + } + +##### Example; good + + double divide(int a, int b) { + Expects(b != 0); // good, address via precondition (and replace with contracts once C++ gets them) + return a/b; + } + + double divide(int a, int b) { + return b ? a/b : quiet_NaN(); // good, address via check + } **Alternative**: For critical applications that can afford some overhead, use a range-checked integer and/or floating-point type. ##### Enforcement -??? +* Flag division by an integral value that could be zero + # PER: Performance