Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-02-05 11:30:06


On Wed, 5 Feb 2003 10:30:01 -0300, "Fernando Cacciola"
<fernando_cacciola_at_[hidden]> wrote:

>
>"Gennaro Prota" <gennaro_prota_at_[hidden]> wrote in message

>> Enumerations are not integral types. Also, the presence of next and
>> prior exposes the use with enumerators to undefined behavior. Do we
>> really want that?
>>
>>
>Good point, however, I think that the *intent* of 'integral_c' is to
>represent values that are 'integral constant expressions', which paricularly
>includes enumerations besides ordinary integral type values.

Yes, that was likely the intent. Actually I was imprecise when talking
about undefined behavior; in general for constant expressions you have
ill-formedness, not UB (at least when the constant expressions shall
be evaluated during translation, as it is the case for N+1 and N-1 in
integral_c). The relevant section of the standard is

  5/5: If during the evaluation of an expression, the result is
       not mathematically defined or not in the range of
       representable values for its type, the behavior is undefined,
       unless such an expression is a constant expression (5.19), in
       which case the program is ill-formed.

[incidentally I recently asked a clarification about it on
comp.std.c++:

http://google.com/groups?threadm=3p511v8da4difcutopta64vceusd9vd7r7%404ax.com
]

Now, it's not clear to me what happens in the following cases, where
an explicit type conversion to enum appears:

1. #include <climits>
    enum e { e1 = 1 };

    int main() {
        char c [e( e1 +1 )];
    }

The sub-expression e1 + 1 is ok and has type int, but what about e( e1
+1 )? Is the result of e1 + 1 in the range of 'representable values'
for the enumeration?

2. Worse than above: this is a case where the result isn't
representable at all in the underlying type: suppose that CHAR_MAX <
LONG_MAX and that your compiler chooses char as underlying type of the
enum:

    #include <climits>
    enum e { e1 = 1 };

    const long lng = CHAR_MAX;
    int main() {
        char c [e( lng + 1 )];
    }

Is the above ill-formed? Or is it UB?

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk