Skip to content

CWG3184 [expr.add] Pointer subtraction with similar pointer types should be allowed #892

@Halalaluyafail3

Description

@Halalaluyafail3

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

Reference (section label): [expr.add]/2

Issue description:

Currently subtraction of two pointers is constrained the same as in C, meaning that only pointers to qualified or unqualified versions of the same complete object type are allowed. This has some bizarre consequences:

static_assert(std::sentinel_for<const char*const*,char**>);
static_assert(!std::sized_sentinel_for<const char*const*,char**>);

const char*const* is a sentinel for char**, but it is not a sized sentinel for char**. There does not appear to be any fundamental reason why this is the case. It is also valid to mix these types in conditional operators, for example:

const char*const*p=0;
char**q=0;
auto r=0?p:q;//deduced type const char*const*

Therefore q-p is invalid but (0?p:q)-p is valid.

Suggested resolution:

Modify [expr.add]/2.2:

both operands are similar pointers to cv-qualified or cv-unqualified versions of the same completely-defined object types; or

Or, if it should be phrased as the types of the pointers being similar:

both operands are pointers to cv-qualified or cv-unqualified versions of the samesimilar completely-defined object types; or

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