Topic: Class templates curiously parametrized by their own pointer-to-members
Author: "Ivan G." <nekotekina@gmail.com>
Date: Sun, 16 Sep 2018 15:01:47 -0700 (PDT)
Raw View
------=_Part_1411_1233671835.1537135307886
Content-Type: multipart/alternative;
boundary="----=_Part_1412_119249185.1537135307886"
------=_Part_1412_119249185.1537135307886
Content-Type: text/plain; charset="UTF-8"
Hello, this is another simple attempt on properties, mixins, and adjacent
problems. It probably doesn't contain anything new, maybe just another
point of view.
The idea is to have special template parameter with special syntax, of
pointer-to-member type, which turns the class template into a special one
which cannot be instantiated outside of the context of another class. It
can strictly become only a class member.
template <auto Prop, auto this::* This>
struct deeper
{
int foo()
{
return z; // Ok: z belongs to property
}
int bar()
{
return static_cast<struct bar*>(this)->y.fu(); // Ok: can reach
higher levels by casting pointer
}
};
template <auto this::* This>
struct property
{
int z;
deeper<This> w;
int fu()
{
// return this->z; // Error: bar doesn't have z
return (this->*This).z; // Ok: 'this' points to bar
}
};
struct bar
{
property<> x; // 'This' is equal to &bar::x
property<> y; // 'This' is equal to &bar::y
// deeper<> inside the property should have zero side, just like empty
base optimization
static_assert(sizeof(x) == sizeof(int));
};
auto this::* is "magical" pointer-to-member, which is normally impossible
and seems absurd at the first glance. However, I believe it's just a minor
technical issue and can be implemented.
Because every member's type is unique, I believe that pointers to such
members can be simply upcasted with static_cast.
Objects of such types obviously cannot be constructed.
Outer class is directly visible from inside of them, but I cannot decide to
what extent, and how it should interact with inner classes' own members.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9fd9c42a-916b-44de-8ab4-82d77df09859%40isocpp.org.
------=_Part_1412_119249185.1537135307886
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello, this is another simple attempt on properties, mixin=
s, and adjacent problems. It probably doesn't contain anything new, may=
be just another point of view.<div><br></div><div>The idea is to have speci=
al template parameter with special syntax, of pointer-to-member type, which=
turns the class template into a special one which cannot be instantiated o=
utside of the context of another class. It can strictly become only a class=
member.<br></div><div><br></div><div><br><div style=3D"color: #000000;back=
ground-color: #ffffff;font-family: 'Droid Sans Mono', 'monospac=
e', monospace, 'Droid Sans Fallback';font-weight: normal;font-s=
ize: 14px;line-height: 19px;white-space: pre;"><div><span style=3D"color: #=
0000ff;">template </span><span style=3D"color: #000000;"><</span><span s=
tyle=3D"color: #0000ff;">auto</span><span style=3D"color: #000000;"> Prop, =
</span><span style=3D"color: #0000ff;">auto</span><span style=3D"color: #00=
0000;"> </span><span style=3D"color: #0000ff;">this</span><span style=3D"co=
lor: #000000;">::</span><span style=3D"color: #000000;">*</span><span style=
=3D"color: #000000;"> This</span><span style=3D"color: #000000;">></span=
></div><div><span style=3D"color: #0000ff;">struct</span><span style=3D"col=
or: #000000;"> </span><span style=3D"color: #267f99;">deeper</span></div><d=
iv><span style=3D"color: #000000;">{</span></div><div><span style=3D"color:=
#000000;"> =C2=A0 =C2=A0</span><span style=3D"color: #0000ff;">int</span><=
span style=3D"color: #000000;"> </span><span style=3D"color: #795e26;">foo<=
/span><span style=3D"color: #000000;">()</span></div><div><span style=3D"co=
lor: #000000;"> =C2=A0 =C2=A0{</span></div><div><span style=3D"color: #0000=
00;"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #af00db;">ret=
urn</span><span style=3D"color: #000000;"> z; </span><span style=3D"color: =
#008000;">// Ok: z belongs to property</span></div><div><span style=3D"colo=
r: #000000;"> =C2=A0 =C2=A0}</span></div><br><div><span style=3D"color: #00=
0000;">=C2=A0 =C2=A0 </span><span style=3D"color: #0000ff;">int</span><span=
style=3D"color: #000000;"> </span><span style=3D"color: #795e26;">bar</spa=
n><span style=3D"color: #000000;">()</span></div><div><span style=3D"color:=
#000000;"> =C2=A0 =C2=A0{</span></div><div><span style=3D"color: #000000;"=
> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #af00db;">return<=
/span><span style=3D"color: #000000;"> </span><span style=3D"color: #0000ff=
;">static_cast</span><span style=3D"color: #000000;"><</span><span style=
=3D"color: #0000ff;">struct</span><span style=3D"color: #000000;"> </span><=
span style=3D"color: #267f99;">bar</span><span style=3D"color: #000000;">*&=
gt;</span><span style=3D"color: #000000;">(</span><span style=3D"color: #00=
00ff;">this</span><span style=3D"color: #000000;">)-></span><span style=
=3D"color: #001080;">y</span><span style=3D"color: #000000;">.</span><span =
style=3D"color: #795e26;">fu</span><span style=3D"color: #000000;">(); </sp=
an><span style=3D"color: #008000;">// Ok: can reach higher levels by castin=
g pointer</span></div><div><span style=3D"color: #000000;"> =C2=A0 =C2=A0}<=
/span></div><div><span style=3D"color: #000000;">};</span></div><br><div><s=
pan style=3D"color: #0000ff;">template </span><span style=3D"color: #000000=
;"><</span><span style=3D"color: #0000ff;">auto</span><span style=3D"col=
or: #000000;"> </span><span style=3D"color: #0000ff;">this</span><span styl=
e=3D"color: #000000;">::</span><span style=3D"color: #000000;">*</span><spa=
n style=3D"color: #000000;"> This</span><span style=3D"color: #000000;">>=
;</span></div><div><span style=3D"color: #0000ff;">struct</span><span style=
=3D"color: #000000;"> </span><span style=3D"color: #267f99;">property</span=
></div><div><span style=3D"color: #000000;">{</span></div><div><span style=
=3D"color: #000000;"> =C2=A0 =C2=A0</span><span style=3D"color: #0000ff;">i=
nt</span><span style=3D"color: #000000;"> z;</span></div><br><div><span sty=
le=3D"color: #000000;">=C2=A0 =C2=A0 deeper</span><span style=3D"color: #00=
0000;"><</span><span style=3D"color: #000000;">This</span><span style=3D=
"color: #000000;">></span><span style=3D"color: #000000;"> w;<br></span>=
</div><br><div><span style=3D"color: #000000;">=C2=A0 =C2=A0 </span><span s=
tyle=3D"color: #0000ff;">int</span><span style=3D"color: #000000;"> </span>=
<span style=3D"color: #795e26;">fu</span><span style=3D"color: #000000;">()=
</span></div><div><span style=3D"color: #000000;"> =C2=A0 =C2=A0{</span></d=
iv><div><span style=3D"color: #000000;"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span>=
<span style=3D"color: #008000;">// return this->z; // Error: bar doesn&#=
39;t have z</span></div><br><div><span style=3D"color: #000000;">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 </span><span style=3D"color: #af00db;">return</span><span=
style=3D"color: #000000;"> (</span><span style=3D"color: #0000ff;">this</s=
pan><span style=3D"color: #000000;">-></span><span style=3D"color: #0000=
00;">*</span><span style=3D"color: #000000;">This).</span><span style=3D"co=
lor: #001080;">z</span><span style=3D"color: #000000;">; </span><span style=
=3D"color: #008000;">// Ok: 'this' points to bar</span></div><div><=
span style=3D"color: #000000;"> =C2=A0 =C2=A0}</span></div><div><span style=
=3D"color: #000000;">};</span></div><br><div><div style=3D"color: #000000;b=
ackground-color: #ffffff;font-family: 'Droid Sans Mono', 'monos=
pace', monospace, 'Droid Sans Fallback';font-weight: normal;fon=
t-size: 14px;line-height: 19px;white-space: pre;"><div><span style=3D"color=
: #0000ff;">struct</span><span style=3D"color: #000000;"> </span><span styl=
e=3D"color: #267f99;">bar</span></div><div><span style=3D"color: #000000;">=
{</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0=
property</span><span style=3D"color: #000000;"><></span><span style=
=3D"color: #000000;"> x; </span><span style=3D"color: #008000;">// 'Thi=
s' is equal to &bar::x</span></div><div><span style=3D"color: #0000=
00;">=C2=A0=C2=A0=C2=A0=C2=A0property</span><span style=3D"color: #000000;"=
><></span><span style=3D"color: #000000;"> y; </span><span style=3D"c=
olor: #008000;">// 'This' is equal to &bar::y</span></div><br><=
div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0</span><span st=
yle=3D"color: #008000;">// deeper<> inside the property should have z=
ero side, just like empty base optimization</span></div><div><span style=3D=
"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #795=
e26;">static_assert</span><span style=3D"color: #000000;">(</span><span sty=
le=3D"color: #0000ff;">sizeof</span><span style=3D"color: #000000;">(x) </s=
pan><span style=3D"color: #000000;">=3D=3D</span><span style=3D"color: #000=
000;"> </span><span style=3D"color: #0000ff;">sizeof</span><span style=3D"c=
olor: #000000;">(</span><span style=3D"color: #0000ff;">int</span><span sty=
le=3D"color: #000000;">));</span></div><div><span style=3D"color: #000000;"=
>};</span></div></div><span style=3D"color: #0000ff;"></span></div></div><b=
r><br>auto this::* is "magical" pointer-to-member, which is norma=
lly impossible and seems absurd at the first glance. However, I believe it&=
#39;s just a minor technical issue and can be implemented.</div><div><br></=
div><div>Because every member's type is unique, I believe that pointers=
to such members can be simply upcasted with static_cast.</div><div><br></d=
iv><div>Objects of such types obviously cannot be constructed.</div><div>Ou=
ter class is directly visible from inside of them, but I cannot decide to w=
hat extent, and how it should interact with inner classes' own members.=
</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9fd9c42a-916b-44de-8ab4-82d77df09859%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9fd9c42a-916b-44de-8ab4-82d77df09859=
%40isocpp.org</a>.<br />
------=_Part_1412_119249185.1537135307886--
------=_Part_1411_1233671835.1537135307886--
.