https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115592

            Bug ID: 115592
           Summary: CWG DR 2823 seems incompletely implemented
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: de34 at live dot cn
  Target Milestone: ---

Per CWG 2823 (https://cplusplus.github.io/CWG/issues/2823.html), dereferencing
a null pointer is UB no matter whether indirect access happens.

So the following code snippet should be rejected, but GCC currently accepts it.

https://godbolt.org/z/15ed7ec8b
```
static_assert([]{
    struct S {};
    *static_cast<S*>(nullptr);
    return true;
}());
```

In the case where a non-static member function is called, GCC starts to reject
the example since 14.

https://godbolt.org/z/Kzjhdqddh
```
static_assert([]{
    struct S {
        constexpr void get() const {}
    };
    static_cast<S*>(nullptr)->get();
    return true;
}());
```

It's curious whether calling a static member function should be rejected.
Per [expr.ref] (after CWG 2813), given a static member `m`, `a` in `a.m` is a
discarded-value expression. But `p` in `p->m` is not - it's semantically
dereferenced even though the result of dereferencing is unused.

Currently GCC also accepts the following example.
https://godbolt.org/z/9hGn7KajT
```
static_assert([]{
    struct S {
        static constexpr void get() {}
    };
    static_cast<S*>(nullptr)->get();
    return true;
}());
```

It's unclear to me whether it's better to consider the pointer to be
dereferenced here, which is currently required and hence results in constant
evaluation failure. If the dereferencing is undesired, perhaps we should submit
a new CWG issue for changing.

Reply via email to