Skip to content

CWG2855 [expr.post.incr] has no integral promotion, [expr.pre.incr] has #479

@leni536

Description

@leni536

Full name of submitter: Lénárd Szolnoki

Reference (section label): [expr.post.incr]

The value of the operand object is modified by adding 1 to it.

Issue description

consider:

int8_t x = 127;
x++;

This has undefined behavior: the value is increased by 1 but the resulting value is not representable in int8_t [expr.pre].

In comparison [expr.pre.incr] in addition has:

The expression ++x is equivalent to x+=1.

Which in turn is equivalent to x = x + 1, except that x is evaluated only once. This implies that integral promotion rules are considered.

Because of this the following is defined:

int8_t x = 127;
++x;

where ++x is equivalent to x = (int)x + 1.

Implementations

GCC, clang and MSVC accept both examples above in constant expressions. They otherwise detect overflowing increments when no promotion is involved.

Suggested resolution

Spell out that the side effects of post-increment are equivalent to the side-effects of pre-increment, with the implied integral promotions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions