Skip to content

[clang] Intentional deviation from the C23 Standard regarding constexpr declarations? #225006

Description

@arlleex

Good afternoon.
According to the C23 standard (ISO/IEC 9899:2024 (en) — working draft N3220, 6.2.1 Scopes of identifiers, type names, and compound literals):

7 Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. An ordinary identifier that has an underspecified definition has scope that starts when the definition is completed; if the same ordinary identifier declares another entity with a scope that encloses the current block, that declaration is hidden as soon as the inner declarator is completed.19) Any other identifier has scope that begins just after the completion of its declarator.

19)That means, that the outer declaration is not visible for the initializer.

Why does Clang allow referencing the identifier name being declared within the initializer for a struct (as expample) (given that the definition is not yet complete)?

constexpr struct {
  int i, j;
} CONSTEXPR_STR = {
  .i = 10,
  .j = CONSTEXPR_STR.i
};

And it is even permitted to use indexing expressions to access array elements:

constexpr struct {
  int i, j;
} CONSTEXPR_STR[2] = {
  [0] = {
    .i = 10,
    .j = CONSTEXPR_STR[0].i
  },
  [1] = {
    .i = CONSTEXPR_STR[0].j,
    .j = CONSTEXPR_STR[0].i * CONSTEXPR_STR[1].i
  }
};

GCC does not accept this code.
And while I prefer Clang’s behavior, I do have a question: was this behavior intentional, or is it a bug that will be fixed in the future? If it is a bug, then unfortunately—at the very least—I will have to rewrite some of my source code, increasing its visual bulk.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clangClang issues not falling into any other category

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions