Skip to content

CWG3004 [expr.const] Pointer arithmetic on an array whose bound is not yet known #680

Description

@frederick-vs-ja

Full name of submitter (unless configured in github; will be published with the issue): Barry Revzin

Reference (section label): [expr.const]

Link to reflector thread (if any):

Issue description:

From P2280R4:

Perhaps the most fun example is this one:

extern const int arr[];
constexpr const int *p = arr + N;
constexpr int arr[2] = {0, 1};
constexpr int k = *p;

Which every compiler currently provides different results (in order of most reasonable to least reasonable):

  1. Clang says arr+N is non-constant if N != 0, and accepts with N == 0.
  2. GCC says arr+N is always constant (even though it sometimes has UB), but rejects reading *p if arr+N is out of bounds.
  3. ICC says arr+N is always constant (even though it sometimes has UB), but always rejects reading *p even if arr+N is in-bounds.
  4. MSVC says you can’t declare arr as non-constexpr and define it constexpr, even though there is no such rule

This, to me, seems like there should be an added rule in [expr.const] that rejects addition and subtraction to an array of unknown bound unless that value is 0. This case seems unrelated enough to the rest of the paper that I think it should just be a Core issue.

It seems that no CWG issue is created for this yet.

Suggested resolution:

Activity

  1. frederick-vs-ja commented on Mar 7, 2025

    @frederick-vs-ja
    Author
  2. t3nsor commented on Mar 9, 2025

    @t3nsor

    There's another gap as well in the wording for arrays of unknown bound. We say in [basic.types.general]

    The declared type of an array object can be an array of unknown bound and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points (“array of unknown bound of T” and “array of N T”) are different types.

    which heavily hints, but does not outright normatively specify, that at a point from which declarations are reachable that give a variable both types "array of unknown bound of T" and "array of N T", the latter is considered to be the type.

  3. jensmaurer commented on Mar 10, 2025

    @jensmaurer
    Member
  4. changed the title [-][expr.const] Pointer arithmetic on an array whose bound is not yet known[/-] [+]CWG3004 [expr.const] Pointer arithmetic on an array whose bound is not yet known[/+] on Mar 10, 2025
  5. jensmaurer commented on Sep 13, 2025

    @jensmaurer
    Member

    CWG3065 for @t3nsor's concern.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions