C++ Logo

std-proposals

Advanced search

Re: Allowing access to object representations

From: Brian Bi <bbi5291_at_[hidden]>
Date: Mon, 19 Aug 2019 11:33:11 -0500
On Sat, Aug 17, 2019 at 2:16 PM Language Lawyer via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> std::bless<std::byte[sizeof(T)]> (or how it is currently named), when it
> is added to the standard, will kinda allow accessing object representation
> without introducing very non-trivial changes to the C++ memory and object
> model.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

If I understand the std::bless approach properly, it has a serious
shortcoming: let's say T has some sufficiently nontrivial class type. Then,
the call to std::bless followed by reading from the std::byte array will
create the std::byte array, which *ends the lifetime of the *T since the T's
storage has now been reused by the std::byte array. If T is not an
implicit-lifetime type, then that means if you still want to use the
original T value *qua* T, then tough luck. If the T object had automatic
storage duration and a nontrivial destructor, then UB will happen when it
goes out of scope.

None of this would be a problem if we could just go back to the C++14
paradigm where we all agreed that every object of type T effectively
overlays objects of type char[T] and unsigned char[T] at the same address
(cf. CWG 1314).

-- 
*Brian Bi*

Received on 2019-08-19 11:35:26