Range of enumerations according to Stroustrup

P

Peter

In Stroustrup's book the following statement can be found:

"The range of an enumeration holds all the enumeration's enumerator values rounded up to the nearest larger binary power minus 1. The range goes down to 0 if the smallest enumerator is non-negative and to the nearest lesser negative binary power if the smallest enumerator is negative".

The statement is followed by an example which contradicts the rule stated above:

enum e3 { min = -10, max = 1000000 }; // range -1048576:1048575

Why would the lower limit be -1048576? The smallest enumerator is -10 and the nearest negative binary power lesser than -10 is -16, not -1048576.
 
Ö

Öö Tiib

In Stroustrup's book the following statement can be found:

"The range of an enumeration holds all the enumeration's enumerator
values rounded up to the nearest larger binary power minus 1. The range
goes down to 0 if the smallest enumerator is non-negative and to the
nearest lesser negative binary power if the smallest enumerator is
negative".

Yes, there is logic error and Stroustrup has published a fix:

Chapter 4:
pg 77 Replace the 5th paragraph with: "An enumerator can be initialized
by a constant-expression (C.5) of integral type (4.1.1). The range of
an enumeration holds all the enumeration's enumerator values rounded up
to the nearest larger binary power minus 1. The range goes down to 0
if the smallest enumerator is non-negative. If the smallest enumerator
is negative, the range goes down to -(1+max) where max is the largest
value in the positive part of the range. This defines the smallest
bit-field capable of holding the enumerator values using the
conventional two's complement representation. For example:"

http://www.stroustrup.com/3rd_printing15.html

Probably that can be also misinterpreted somehow however.
 
P

ptyxs

Le 14/09/2013 20:55, Peter a écrit :
In Stroustrup's book the following statement can be found:

"The range of an enumeration holds all the enumeration's enumerator values rounded up to the nearest larger binary power minus 1. The range goesdown to 0 if the smallest enumerator is non-negative and to the nearest lesser negative binary power if the smallest enumerator is negative".

The statement is followed by an example which contradicts the rule stated above:

enum e3 { min = -10, max = 1000000 }; // range -1048576:1048575

Why would the lower limit be -1048576? The smallest enumerator is -10 and the nearest negative binary power lesser than -10 is -16, not -1048576.

Stroustrup's book : you should precise which book you are quoting !!
Stroustrup did'nt write juste one !

Note that present day's C++ reference book by Stroustrup is The C++
Programming Language 4th edition (may 2013), not 3rd edition

Note, soon to appear : A tour of C++

ptyxs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top