[llvm-branch-commits] [libc++] Introduce __force_nonstandard_layout base class for pointer field protection (PR #151652)

A. Jiang via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Aug 2 00:21:37 PDT 2025


frederick-vs-ja wrote:

> That's right. For example, the standard would require that a standard-layout `std::unique_ptr<int>` has the same representation as `int *` (assuming the obvious implementation), and it was not considered practical to change the representation of all pointers for compatibility reasons.

However, even if we make `std::unique_ptr<int>` non-standard-layout, as long as the `int*` subobject is at offset 0, it's still well-defined to access the `int*` subobject via `*std::launder(reinterpret_cast<int*>(&up))`. I'm not sure whether merely requiring `launder` would be safe enough.

I think a "safer" way (defending against `reinterpret_cast`) would be making the `int*` subobject (and its friends) at some positive offset. But this is theoretically orthogonal to the standard-layout property.

https://github.com/llvm/llvm-project/pull/151652


More information about the llvm-branch-commits mailing list