C++ Logo


Advanced search

Re: [std-proposals] An issue with IOC

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sun, 2 Oct 2022 11:15:38 -0400
On Sun, Oct 2, 2022 at 10:37 AM blacktea hamburger via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> However, the implicitly created object does end the lifetime of the original char objects. Although it can be accessed by char, the result of the access is no longer the original char objects' value (it's also not accessed by char).

The `char` doesn't have an "original" value. Remember: the objects
were created immediately after `new char[]` was invoked. There was
*always* a `Foo` or a `Bar` there. That is, the code is (mostly)
equivalent to this:

char arr[N];
auto *pFoo = new(arr) Foo;
//Copy into pFoo;

if(*reinterpret_cast<char*>(pFoo) == FOO)
{ ... }

As such, this code is exactly as legitimate as:

Foo foo = //initialize.
auto byte = *reinterpret_cast<char*>(&foo);

This code is intended to access the object representation of `foo`.
This is precisely why [basic.lval]/11 has an explicit carveout from
aliasing for `char` and other bytewise types. If there is something in
the standard which renders this code UB, then that would be considered
a defect.

Received on 2022-10-02 15:16:55