Search code examples
cenumslanguage-lawyerarithmetic-expressions

Does x * x (and similar) lead to constraint violation if x has enumerated type?


C11, 6.2.5 Types, 18:

Integer and floating types are collectively called arithmetic types.

C11, 6.2.5 Types, 16:

An enumeration comprises a set of named integer constant values. Each distinct enumeration constitutes a different enumerated type.

C11, 6.5.5 Multiplicative operators, Constraints, 2 (emphasis added):

Each of the operands shall have arithmetic type.

Sample code:

enum E { a };
int f(enum E x)
{
        return x * x;
}

Invocations:

$ gcc t0.c -std=c11 -pedantic -Wall -Wextra -c
<nothing>

$ clang t0.c -std=c11 -pedantic -Wall -Wextra -c
<nothing>

$ icc t0.c -std=c11 -pedantic -Wall -Wextra -c
<nothing>

$ cl t235.c /std:c11 /Za /c
<nothing>

Question: Does x * x (and similar) lead to constraint violation?

As I understand, the operand x has enumerated type, not arithmetic type.


Solution

  • Enumerated types are integer types, which are arithmetic types. There is no constraint violation.

    From n1548: 6.2.5.17

    The type char, the signed and unsigned integer types, and the enumerated types are collectively called integer types.

    "Integer types" are arithmetic types, as you noted.