Fix Enum.4 example

Issue #716
This commit is contained in:
Bjarne Stroustrup 2017-06-12 17:07:50 -04:00
parent 5393fab88f
commit 8cc76e2c38

View File

@ -8436,16 +8436,26 @@ Convenience of use and avoidance of errors.
##### Example ##### 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<Day>(static_cast<int>(d)+1);
} }
Day today = Day::sat; Day today = Day::sat;
Day tomorrow = ++today; 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 ##### Enforcement
Flag repeated expressions cast back into an enumeration. Flag repeated expressions cast back into an enumeration.