Date: Fri, 24 Apr 2026 18:13:08 +0200
Does
P0149R3 Generalised member pointers (Jeff Snyder)
help with this?
Jens
On 4/24/26 17:56, Phil Endecott via Std-Proposals wrote:
> Dear Experts,
>
> Say I have this:
>
> struct M {
> bool ax;
> bool ay
> bool bx;
> bool by;
> };
>
> I decide I'm going to refactor that to add some hierarchy:
>
> struct S {
> bool x;
> bool y;
> };
>
> struct new_M {
> S a;
> S b;
> };
>
> I replace code that used to say ".ax" with ".a.x", etc.
>
> All is good, except that somewhere I take a pointer to a member of M:
>
> void toggle(M* m, bool M::* p)
> {
> m->*p = !(m->*p);
> }
>
>
> Both the old M and the new_M actually have the same layout in memory.
> This makes me wonder if it's possible to have a "deep" pointer-to-data-member.
> Since this is the simple case with no inheritance or virtual methods, the
> pointer-to-members are just offsets into the struct (right?). So I feel that
> I should be able to create a pointer that refers to new_M.a.x:
>
> bool M::* ptr = &(new_M.a::x);
>
> Is there some flaw that makes this impossible to implement? Or maybe there
> is some existing way to do it?
>
> Maybe....
>
> auto ptr = reinterpret_cast<bool M::*>( offsetof(M,a) + offsetof(S,x) );
>
> ????
>
>
> Thoughts anyone?
>
> Thanks, Phil.
>
>
P0149R3 Generalised member pointers (Jeff Snyder)
help with this?
Jens
On 4/24/26 17:56, Phil Endecott via Std-Proposals wrote:
> Dear Experts,
>
> Say I have this:
>
> struct M {
> bool ax;
> bool ay
> bool bx;
> bool by;
> };
>
> I decide I'm going to refactor that to add some hierarchy:
>
> struct S {
> bool x;
> bool y;
> };
>
> struct new_M {
> S a;
> S b;
> };
>
> I replace code that used to say ".ax" with ".a.x", etc.
>
> All is good, except that somewhere I take a pointer to a member of M:
>
> void toggle(M* m, bool M::* p)
> {
> m->*p = !(m->*p);
> }
>
>
> Both the old M and the new_M actually have the same layout in memory.
> This makes me wonder if it's possible to have a "deep" pointer-to-data-member.
> Since this is the simple case with no inheritance or virtual methods, the
> pointer-to-members are just offsets into the struct (right?). So I feel that
> I should be able to create a pointer that refers to new_M.a.x:
>
> bool M::* ptr = &(new_M.a::x);
>
> Is there some flaw that makes this impossible to implement? Or maybe there
> is some existing way to do it?
>
> Maybe....
>
> auto ptr = reinterpret_cast<bool M::*>( offsetof(M,a) + offsetof(S,x) );
>
> ????
>
>
> Thoughts anyone?
>
> Thanks, Phil.
>
>
Received on 2026-04-24 16:13:12
