C++ Logo

std-discussion

Advanced search

Is the intent of [class.cdtor] p2 to disallow aliasing, and is the wording strong enough to be useful?

From: Jan Schultke <janschultke_at_[hidden]>
Date: Sat, 5 Aug 2023 01:01:47 +0200
The issue starts with the following code gen: https://godbolt.org/z/PYTohPTKr

Here, adding `__restrict` improves the code gen, but it doesn't for
GCC. GCC seems to assume that no aliasing can take place between
`this` and other parameters passed into the function. LLVM does not
add `noalias` without `__restrict`.

[class.cdtor] p2 says:
> During the construction of an object, if the value of the
> object or any of its subobjects is accessed through a
> glvalue that is not obtained, directly or indirectly,
> from the constructor's this pointer, the value
> of the object or subobject thus obtained is unspecified.

I believe that this paragraph's intent is to disallow aliasing, e.g.
between `this` and rvalue reference passed into the move constructor.
However, the wording is very difficult to implement.
- GCC simply assumes `noalias`, which also assumes that writing to the
aforementioned glvlaue will make its value unspecified. The paragraph
only talks about the obtained value though.
- clang does not assume anything, and possibly misses optimizations as a result.

Am I interpreting the intent correctly? If so, how can the wording be
improved to fully disallow aliasing, as intended, so that we can just
add `noalias` here, as is already done?

Received on 2023-08-04 23:02:00