From 8cc76e2c38f5f3092079483dde9959766031ea20 Mon Sep 17 00:00:00 2001 From: Bjarne Stroustrup Date: Mon, 12 Jun 2017 17:07:50 -0400 Subject: [PATCH] Fix Enum.4 example Issue #716 --- CppCoreGuidelines.md | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index a55eb70..e46015d 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -8436,16 +8436,26 @@ Convenience of use and avoidance of errors. ##### Example - enum class Day { mon, tue, wed, thu, fri, sat, sun }; + enum Day { mon, tue, wed, thu, fri, sat, sun }; - Day operator++(Day& d) + Day& operator++(Day& d) { - return d == Day::sun ? Day::mon : Day{++d}; + return d = (d==Day::sun) ? Day::mon : static_cast(static_cast(d)+1); } - + Day today = Day::sat; Day tomorrow = ++today; +The use of a `static_cast` is not pretty, but + + Day& operator++(Day& d) + { + return d = (d== Day::sun) ? Day::mon : Day{++d}; // error + } + +is an infinite recursion, and writing it without a cast, using a `switch` on all cases is longwinded. + + ##### Enforcement Flag repeated expressions cast back into an enumeration.