Topic: Improve [[no_unique_address]] - make addresses equal
Author: "Ivan G." <nekotekina@gmail.com>
Date: Wed, 22 Aug 2018 12:54:51 -0700 (PDT)
Raw View
------=_Part_370_1651081991.1534967691926
Content-Type: multipart/alternative;
boundary="----=_Part_371_1587381720.1534967691927"
------=_Part_371_1587381720.1534967691927
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Hello, this is not really a proposal but rather a bunch of questions.
First, could standard attribute even enforce something, or attributes are=
=20
simply not suitable for this, judging by the fact they are silently ignored=
=20
in C++17?
The idea is simple - if data members with [[no_unique_address]] are empty,=
=20
their exact location doesn't matter, why not always assume them at the=20
beginning of the class, essentially reusing its base address, in other=20
words, `this` pointer?
The attendant idea is to allow reinterpret_cast it back to the type which=
=20
contains the data member with [[no_unique_address]], and use it for:
1. Emulating "properties" in more advanced manner
2. Other things involving CRTP which are usually done via inheritance, but=
=20
in theory don't really need inheritance for their purposes.
Rough example of "property x" implementation, it has more advantages over=
=20
current possibilities because the nested class now has full access to the=
=20
base class:
=D0=92=D0=B2=D0=B5=D0=B4=D0=B8=D1=82=D0=B5
class base
{
unsigned m_x;
public:
void set_x(unsigned x)
{
m_x =3D x + 1;
}
unsigned get_x() const
{
return m_x - 1;
}
[[no_unique_address]] struct prop_x
{
unsigned& operator=3D(unsigned arg)
{
reinterpret_cast<base*>(this)->set_x(arg);
}
operator unsigned() const
{
return reinterpret_cast<const base*>(this)->get_x();
}
} x;
};
=D0=BA=D0=BE=D0=B4...
--=20
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 e=
mail 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/5d8b472a-86b3-4bee-a7a6-b67bbc4bae47%40isocpp.or=
g.
------=_Part_371_1587381720.1534967691927
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello, this is not really a proposal but rather a bunch of=
questions.<div><br></div><div>First, could standard attribute even enforce=
something, or attributes are simply not suitable for this, judging by the =
fact they are silently ignored in C++17?</div><div><br></div><div>The idea =
is simple - if data members with [[no_unique_address]] are empty, their exa=
ct location doesn't matter, why not always assume them at the beginning=
of the class, essentially reusing its base address, in other words, `this`=
pointer?</div><div><br></div><div>The attendant idea is to allow reinterpr=
et_cast it back to the type which contains the data member with [[no_unique=
_address]], and use it for:</div><div>1. Emulating "properties" i=
n more advanced manner</div><div>2. Other things involving CRTP which are u=
sually done via inheritance, but in theory don't really need inheritanc=
e for their purposes.</div><div><br></div><div>Rough example of "prope=
rty x" implementation, it has more advantages over current possibiliti=
es because the nested class now has full access to the base class:</div><di=
v><br></div><div><div style=3D"background-color: #FAFAFA; border-color: #BB=
B; border-style: solid; border-width: 1px; word-wrap: break-word;" class=3D=
"prettyprint"><code class=3D"prettyprint"><div class=3D"subprettyprint"><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">=D0=92=D0=B2=D0=B5=
=D0=B4=D0=B8=D1=82=D0=B5</span><div style=3D"color: #000000;background-colo=
r: #ffffff;font-family: 'Droid Sans Mono', 'monospace', mon=
ospace, 'Droid Sans Fallback';font-weight: normal;font-size: 14px;l=
ine-height: 19px;white-space: pre;"><div><span style=3D"color: #0000ff;"><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">class</span></span>=
<span style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span></span><span style=3D"color: #267f99;"><span style=
=3D"color: #008;" class=3D"styled-by-prettify">base</span></span></div><div=
><span style=3D"color: #000000;"><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">{</span></span></div><div><span style=3D"color: #000000;"><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</sp=
an></span><span style=3D"color: #0000ff;"><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">unsigned</span></span><span style=3D"color: #00000=
0;"><span style=3D"color: #000;" class=3D"styled-by-prettify"> m_x</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span></span></di=
v><div><span style=3D"color: #0000ff;"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">public</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">:</span></span></div><div><span style=3D"color: #000000=
;"><span style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0=
</span></span><span style=3D"color: #0000ff;"><span style=3D"color: #008;" =
class=3D"styled-by-prettify">void</span></span><span style=3D"color: #00000=
0;"><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span></spa=
n><span style=3D"color: #795e26;"><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">set_x</span></span><span style=3D"color: #000000;"><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span></span><span sty=
le=3D"color: #0000ff;"><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">unsigned</span></span><span style=3D"color: #000000;"><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> x</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">)</span></span></div><div><span style=3D=
"color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> =C2=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">{</span></span></div><div><span style=3D"color: #000000;"><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0=
m_x </span></span><span style=3D"color: #000000;"><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">=3D</span></span><span style=3D"color: #00=
0000;"><span style=3D"color: #000;" class=3D"styled-by-prettify"> x </span>=
</span><span style=3D"color: #000000;"><span style=3D"color: #660;" class=
=3D"styled-by-prettify">+</span></span><span style=3D"color: #000000;"><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> </span></span><span =
style=3D"color: #09885a;"><span style=3D"color: #066;" class=3D"styled-by-p=
rettify">1</span></span><span style=3D"color: #000000;"><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">;</span></span></div><div><span styl=
e=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"> =C2=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">}</span></span></div><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br></span><div><span style=3D"color: #000000;"><span style=3D=
"color: #000;" class=3D"styled-by-prettify">=C2=A0 =C2=A0 </span></span><sp=
an style=3D"color: #0000ff;"><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">unsigned</span></span><span style=3D"color: #000000;"><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span></span><span style=
=3D"color: #795e26;"><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">get_x</span></span><span style=3D"color: #000000;"><span style=3D"color=
: #660;" class=3D"styled-by-prettify">()</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span></span><span style=3D"color: #0000ff;=
"><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span></s=
pan></div><div><span style=3D"color: #000000;"><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">{</span></span></div><div><span style=3D"=
color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-prettify">=
=C2=A0 =C2=A0 =C2=A0 =C2=A0</span></span><span style=3D"color: #af00db;"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">return</span></spa=
n><span style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> m_x </span></span><span style=3D"color: #000000;"><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">-</span></span><span sty=
le=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span></span><span style=3D"color: #09885a;"><span style=3D"color: =
#066;" class=3D"styled-by-prettify">1</span></span><span style=3D"color: #0=
00000;"><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><=
/span></div><div><span style=3D"color: #000000;"><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">}</span></span></div><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color=
: #000000;"><span style=3D"color: #000;" class=3D"styled-by-prettify">=C2=
=A0 =C2=A0 </span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>[[</span><span style=3D"color: #000;" class=3D"styled-by-prettify">no_uniq=
ue_address</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
]]</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span>=
</span><span style=3D"color: #0000ff;"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">struct</span></span><span style=3D"color: #000000;"=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span></span><=
span style=3D"color: #267f99;"><span style=3D"color: #000;" class=3D"styled=
-by-prettify">prop_x</span></span><div><span style=3D"color: #000000;"><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">{</span></span></=
div><div><span style=3D"color: #000000;"><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span></span><span sty=
le=3D"color: #0000ff;"><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">unsigned</span></span><span style=3D"color: #000000;"><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&</span></span><span style=
=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span></span><span style=3D"color: #af00db;"><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">operator</span></span><span style=3D"colo=
r: #000000;"><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D<=
/span></span><span style=3D"color: #000000;"><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span></span><span style=3D"color: #0000ff;">=
<span style=3D"color: #008;" class=3D"styled-by-prettify">unsigned</span></=
span><span style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> arg</span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">)</span></span></div><div><span style=3D"color: #000000;"><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=
=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
{</span></span></div><div><span style=3D"color: #000000;"><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0</span></span><span style=3D"color: #0000ff;"><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">reinterpret_cast</span></span><span =
style=3D"color: #000000;"><span style=3D"color: #660;" class=3D"styled-by-p=
rettify"><</span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">b</span></span><span style=3D"color: #000000;"><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">ase</span></span><span style=3D"color: #00=
0000;"><span style=3D"color: #660;" class=3D"styled-by-prettify">*></spa=
n></span><span style=3D"color: #000000;"><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span></span><span style=3D"color: #0000ff;"><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">this</span></span><sp=
an style=3D"color: #000000;"><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">)-></span></span><span style=3D"color: #795e26;"><span style=
=3D"color: #000;" class=3D"styled-by-prettify">set_x</span></span><span sty=
le=3D"color: #000000;"><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">arg=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><=
/span></div><div><span style=3D"color: #000000;"><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span></span></div><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><div><span=
style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styled-by-=
prettify">=C2=A0 =C2=A0 =C2=A0 =C2=A0 </span></span><span style=3D"color: #=
af00db;"><span style=3D"color: #008;" class=3D"styled-by-prettify">operator=
</span></span><span style=3D"color: #000000;"><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span></span><span style=3D"color: #0000ff;"=
><span style=3D"color: #008;" class=3D"styled-by-prettify">unsigned</span><=
/span><span style=3D"color: #000000;"><span style=3D"color: #660;" class=3D=
"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> </span></span><span style=3D"color: #0000ff;"><span style=3D"=
color: #008;" class=3D"styled-by-prettify">const</span></span></div><div><s=
pan style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styled-=
by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">{</span></span></div><div><span style=3D"col=
or: #000000;"><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0</span></span><span style=3D"color=
: #af00db;"><span style=3D"color: #008;" class=3D"styled-by-prettify">retur=
n</span></span><span style=3D"color: #000000;"><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> </span></span><span style=3D"color: #0000ff;=
"><span style=3D"color: #008;" class=3D"styled-by-prettify">reinterpret_cas=
t</span></span><span style=3D"color: #000000;"><span style=3D"color: #660;"=
class=3D"styled-by-prettify"><</span></span><span style=3D"color: #0000=
ff;"><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span>=
</span><span style=3D"color: #000000;"><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">base</span></span><span style=3D"color: #000000;"><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">*></span></span><span st=
yle=3D"color: #000000;"><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span></span><span style=3D"color: #0000ff;"><span style=3D"color:=
#008;" class=3D"styled-by-prettify">this</span></span><span style=3D"color=
: #000000;"><span style=3D"color: #660;" class=3D"styled-by-prettify">)->=
;</span></span><span style=3D"color: #795e26;"><span style=3D"color: #000;"=
class=3D"styled-by-prettify">get_x</span></span><span style=3D"color: #000=
000;"><span style=3D"color: #660;" class=3D"styled-by-prettify">();</span><=
/span></div><div><span style=3D"color: #000000;"><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> =C2=A0 =C2=A0 =C2=A0 =C2=A0</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span></span></div><div=
><span style=3D"color: #000000;"><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> =C2=A0 =C2=A0</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> x</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">;</span></span></div><div><span style=3D"color: #000000;"><span style=3D=
"color: #660;" class=3D"styled-by-prettify">};</span></span></div></div><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">=D0=BA=D0=BE=D0=B4...</span>=
</div></code></div></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/5d8b472a-86b3-4bee-a7a6-b67bbc4bae47%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5d8b472a-86b3-4bee-a7a6-b67bbc4bae47=
%40isocpp.org</a>.<br />
------=_Part_371_1587381720.1534967691927--
------=_Part_370_1651081991.1534967691926--
.