Topic: Allow applying [[no_unique_address]] to classes?


Author: "Ivan G." <nekotekina@gmail.com>
Date: Thu, 13 Sep 2018 05:51:14 -0700 (PDT)
Raw View
------=_Part_288_923764721.1536843074339
Content-Type: multipart/alternative;
 boundary="----=_Part_289_492883408.1536843074340"

------=_Part_289_492883408.1536843074340
Content-Type: text/plain; charset="UTF-8"

Hello, this would be a really little addition. Has it been discussed? Maybe
there are reasons why this is undesirable.

If a class is marked with [[no_unique_address]], then:
1) All members of this type behave like they were declared with
[[no_unique_address]] attribute.
2) If the class is not empty, the compiler is encouraged to issue a warning.

This could simplify writing code where there are lots of empty structures.
Also the following use case would be more correct in my opinion, if the
[[no_unique_address]] is applied to the class, not the data member.

enum class dynamic_tag_t {};
constexpr dynamic_tag_t dynamic_tag{};

template <typename T, auto V>
[[no_unique_address]] struct const_or_value
{
    static constexpr T value = V;

    const_or_value(T)
    {
    }

    constexpr operator T() const
    {
        return value;
    }
};

template <typename T>
struct const_or_value<T, dynamic_tag>
{
    const T value;

    const_or_value(T value)
        : value(value)
    {
    }

    constexpr operator T() const
    {
        return value;
    }
};

template <auto DSize = dynamic_tag>
class vector_t
{
[[no_unique_address]] const_or_value<uint, DSize> m_size;

public:
    // Constructor is not necessarily correct
vector_t(uint _size = DSize)
: m_size(_size)
{
}
};

--
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/7c83aa4f-6bb0-4ef2-89ed-a23257a46838%40isocpp.org.

------=_Part_289_492883408.1536843074340
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hello, this would be a really little addition. Has it been=
 discussed? Maybe there are reasons why this is undesirable.<div><br></div>=
<div>If a class is marked with [[no_unique_address]], then:<br></div><div>1=
) All members of this type behave like they were declared with [[no_unique_=
address]] attribute.</div><div>2) If the class is not empty, the compiler i=
s encouraged to issue a warning.</div><div><br></div><div>This could simpli=
fy writing code where there are lots of empty structures.</div><div>Also th=
e following use case would be more correct in my opinion, if the [[no_uniqu=
e_address]] is applied to the class, not the data member.</div><div><br></d=
iv><div><div style=3D"color: #000000;background-color: #ffffff;font-family:=
 &#39;Droid Sans Mono&#39;, &#39;monospace&#39;, monospace, &#39;Droid Sans=
 Fallback&#39;;font-weight: normal;font-size: 14px;line-height: 19px;white-=
space: pre;"><div><span style=3D"color: #0000ff;">enum</span><span style=3D=
"color: #000000;"> </span><span style=3D"color: #0000ff;">class</span><span=
 style=3D"color: #000000;"> </span><span style=3D"color: #267f99;">dynamic_=
tag_t</span><span style=3D"color: #000000;"> {};</span></div><div><span sty=
le=3D"color: #0000ff;">constexpr</span><span style=3D"color: #000000;"> </s=
pan><span style=3D"color: #267f99;">dynamic_tag_t</span><span style=3D"colo=
r: #000000;"> dynamic_tag{};</span></div><br><div><span style=3D"color: #00=
00ff;">template </span><span style=3D"color: #000000;">&lt;</span><span sty=
le=3D"color: #0000ff;">typename</span><span style=3D"color: #000000;"> T, <=
/span><span style=3D"color: #0000ff;">auto</span><span style=3D"color: #000=
000;"> V</span><span style=3D"color: #000000;">&gt;</span></div><div><span =
style=3D"color: #000000;">[[no_unique_address]] </span><span style=3D"color=
: #0000ff;">struct</span><span style=3D"color: #000000;"> </span><span styl=
e=3D"color: #267f99;">const_or_value</span></div><div><span style=3D"color:=
 #000000;">{</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=
=C2=A0=C2=A0</span><span style=3D"color: #0000ff;">static</span><span style=
=3D"color: #000000;"> </span><span style=3D"color: #0000ff;">constexpr</spa=
n><span style=3D"color: #000000;"> T value </span><span style=3D"color: #00=
0000;">=3D</span><span style=3D"color: #000000;"> V;</span></div><br><div><=
span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0</span><span style=
=3D"color: #795e26;">const_or_value</span><span style=3D"color: #000000;">(=
T)</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=
=A0{</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=
=A0}</span></div><br><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=
=A0=C2=A0</span><span style=3D"color: #0000ff;">constexpr</span><span style=
=3D"color: #000000;"> </span><span style=3D"color: #af00db;">operator</span=
><span style=3D"color: #000000;"> </span><span style=3D"color: #795e26;">T<=
/span><span style=3D"color: #000000;">() </span><span style=3D"color: #0000=
ff;">const</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=
=A0=C2=A0{</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #af00db;">ret=
urn</span><span style=3D"color: #000000;"> value;</span></div><div><span st=
yle=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0}</span></div><div><span st=
yle=3D"color: #000000;">};</span></div><br><div><span style=3D"color: #0000=
ff;">template </span><span style=3D"color: #000000;">&lt;</span><span style=
=3D"color: #0000ff;">typename</span><span style=3D"color: #000000;"> T</spa=
n><span style=3D"color: #000000;">&gt;</span></div><div><span style=3D"colo=
r: #0000ff;">struct</span><span style=3D"color: #000000;"> </span><span sty=
le=3D"color: #267f99;">const_or_value</span><span style=3D"color: #000000;"=
>&lt;T, dynamic_tag&gt;</span></div><div><span style=3D"color: #000000;">{<=
/span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0</=
span><span style=3D"color: #0000ff;">const</span><span style=3D"color: #000=
000;"> T value;</span></div><br><div><span style=3D"color: #000000;">=C2=A0=
=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #795e26;">const_or_value</sp=
an><span style=3D"color: #000000;">(T value)</span></div><div><span style=
=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0: </sp=
an><span style=3D"color: #795e26;">value</span><span style=3D"color: #00000=
0;">(value)</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=
=C2=A0=C2=A0{</span></div><div><span style=3D"color: #000000;">=C2=A0=C2=A0=
=C2=A0=C2=A0}</span></div><br><div><span style=3D"color: #000000;">=C2=A0=
=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #0000ff;">constexpr</span><s=
pan style=3D"color: #000000;"> </span><span style=3D"color: #af00db;">opera=
tor</span><span style=3D"color: #000000;"> </span><span style=3D"color: #79=
5e26;">T</span><span style=3D"color: #000000;">() </span><span style=3D"col=
or: #0000ff;">const</span></div><div><span style=3D"color: #000000;">=C2=A0=
=C2=A0=C2=A0=C2=A0{</span></div><div><span style=3D"color: #000000;">=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #af0=
0db;">return</span><span style=3D"color: #000000;"> value;</span></div><div=
><span style=3D"color: #000000;">=C2=A0=C2=A0=C2=A0=C2=A0}</span></div><div=
><span style=3D"color: #000000;">};</span></div><br><div><span style=3D"col=
or: #0000ff;">template </span><span style=3D"color: #000000;">&lt;</span><s=
pan style=3D"color: #0000ff;">auto</span><span style=3D"color: #000000;"> D=
Size </span><span style=3D"color: #000000;">=3D</span><span style=3D"color:=
 #000000;"> dynamic_tag</span><span style=3D"color: #000000;">&gt;</span></=
div><div><span style=3D"color: #0000ff;">class</span><span style=3D"color: =
#000000;"> </span><span style=3D"color: #267f99;">vector_t</span></div><div=
><span style=3D"color: #000000;">{</span></div><div><span style=3D"color: #=
000000;">    [[no_unique_address]] const_or_value</span><span style=3D"colo=
r: #000000;">&lt;</span><span style=3D"color: #267f99;">uint</span><span st=
yle=3D"color: #000000;">, DSize</span><span style=3D"color: #000000;">&gt;<=
/span><span style=3D"color: #000000;"> m_size;</span></div><br><div><span s=
tyle=3D"color: #0000ff;">public:</span></div><div><span style=3D"color: #00=
0000;">=C2=A0=C2=A0=C2=A0=C2=A0</span><span style=3D"color: #008000;">// Co=
nstructor is not necessarily correct</span></div><div><span style=3D"color:=
 #000000;">    </span><span style=3D"color: #795e26;">vector_t</span><span =
style=3D"color: #000000;">(</span><span style=3D"color: #267f99;">uint</spa=
n><span style=3D"color: #000000;"> _size </span><span style=3D"color: #0000=
00;">=3D</span><span style=3D"color: #000000;"> DSize)</span></div><div><sp=
an style=3D"color: #000000;">        : </span><span style=3D"color: #795e26=
;">m_size</span><span style=3D"color: #000000;">(_size)</span></div><div><s=
pan style=3D"color: #000000;">    {</span></div><div><span style=3D"color: =
#000000;">    }</span></div><div><span style=3D"color: #000000;">};</span><=
/div></div><span class=3D"hljs-keyword" style=3D"border: 0px; font-family: =
Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; font-size=
: 14px; font-style: normal; font-weight: normal; margin: 0px; padding: 0px;=
 vertical-align: baseline; outline: 0px; color: rgb(133, 153, 0); font-vari=
ant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; o=
rphans: 2; text-align: start; text-indent: 0px; text-transform: none; white=
-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: =
0px;"></span><br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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/7c83aa4f-6bb0-4ef2-89ed-a23257a46838%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7c83aa4f-6bb0-4ef2-89ed-a23257a46838=
%40isocpp.org</a>.<br />

------=_Part_289_492883408.1536843074340--

------=_Part_288_923764721.1536843074339--

.