From 3bc2c048199051a5cad6abb4788502866d849cdc Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 25 May 2016 15:50:11 -0600 Subject: [PATCH] Add note about underflow Closes #47 --- 03-Style.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/03-Style.md b/03-Style.md index 137889d..f9e1f91 100644 --- a/03-Style.md +++ b/03-Style.md @@ -296,6 +296,16 @@ In general, using `auto` will avoid most of these issues, but not all. Make sure you stick with the correct integer types and remain consistent with the C++ standard library. It might not warn on the platform you are currently using, but it probably will when you change platforms. +*Note that you can cause integer underflow when peforming some operations on unsigned values. For example:* + +```cpp +std::vector v1{2,3,4,5,6,7,8,9}; +std::vector v2{9,8,7,6,5,4,3,2,1}; +const auto s1 = v1.size(); +const auto s2 = v2.size(); +const auto diff = s1 - s2; // diff underflows to a very large number +``` + ## Use .hpp and .cpp for Your File Extensions Ultimately this is a matter of preference, but .hpp and .cpp are widely recognized by various editors and tools. So the choice is pragmatic. Specifically, Visual Studio only automatically recognizes .cpp and .cxx for C++ files, and Vim doesn't necessarily recognize .cc as a C++ file.