Skip to content

[expr.const] Pointer arithmetic on an unspecified object during constant evaluation #697

Open
@frederick-vs-ja

Description

@frederick-vs-ja

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

Reference (section label): [expr.add], [expr.const]

Link to reflector thread (if any):

Issue description:

In some circumstances, it is impossible to know whether a pointer arithmetic operation is well-defined if an unknown reference is involved during constant evaluation.

E.g. in the following example:

extern int& r; // defined in another TU
constexpr auto diff = &r + 42 - &r;

The reference r might bind to a non-array-element object, or the first element to an int[1] or int[42] array, which affects the well-definedness of &r + 42 - &r, but we can't know this during constant-initializing diff.

Perhaps we want some conservative rule for such cases.

Suggested resolution:

Modify [expr.const] as indicated:

  1. [...] For such a reference that is not usable in constant expressions, the reference is treated as binding to an unspecified object of the referenced type whose lifetime and that of all subobjects includes the entire constant evaluation and whose dynamic type is constexpr-unknown. For the purpose of constant evaluation, the unspecified object is considered not to be an array element.

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