C++ Logo

std-proposals

Advanced search

Re: P1839 and the object representation of subobjects

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 22 Jul 2020 09:38:34 -0700
On Tuesday, 21 July 2020 23:58:41 PDT Jason McKesson via Std-Proposals wrote:
> Nesting becomes a bit cumbersome to handle:
>
> ```
> struct Inner
> {
> int i;
> };
>
> struct Outer
> {
> Inner in;
> };
>
> Outer o;
> back_to_object<&Outer::in>(back_to_object<&Inner::i>(&o.in.i)) == &o;
> ```

That looks the right amount of cumbersome for a feature that isn't expected to
be used without care.

> But overall, it'd make a lot more sense to just make the tweaks I
> suggested to P1839, which makes `reinterpret_cast`ing to an object
> representation essentially give you a pointer to the start of that
> object's representation within the array of the largest containing
> contiguous object. After all, if you can go down into nested types, it
> makes sense that you could go "up" too.

No doubt. I wasn't proposing to choose one or the other.

But by adding this library function, we remove the need for the offsetof
macro. The function uses only C++ constructs.

> Given the above changes, it's more reasonable to just allow an NSDM
> member pointer to be converted to an offset into its nearest
> subobject. So the above becomes:
>
> ```
> constexpr auto offset = offset_of<&Outer::in, &Inner::i>;
> ```
>
> Where `offset_of` is a variable template that takes a pack of member
> pointers, as described above.

Sounds good, but I still think a function that applies the offset and does the
proper laundering is recommended.

Another thing we can have is a pointer-to-member combiner. That is, for each
pair

  C2 C1::*
  C3 C2::*

it combines and returns

  C3 C1::*

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2020-07-22 11:41:55