Topic: Allow the use of references in combination with unions
Author: Antonio Perez <antonio@perezexcelsior.com>
Date: Mon, 1 Aug 2016 09:22:07 -0700 (PDT)
Raw View
------=_Part_3817_1270612616.1470068527541
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Under the language specification, a structure can store a reference to a va=
riable. Why can't a union?
struct MyStruct
{
double& x;
MyStruct(double* ptr) : x(*ptr) {}
MyStruct(double& ref) : x(ref) {}
void AddOne() { x++; }
void MulTwo() { x*=3D2; }
};
I've been using the GCC C++ compiler on linux, and it allowed me to create =
unions that stored references without issue, as seen in the below code. How=
ever when I attempted to use a different compiler, I got errors because "a =
reference is an illegal union member".=20
Given that structures can already store references without issue, and that =
unions can too for widely-used compilers such as GNU's c++ compiler, why no=
t add this functionality to unions as part of the language specification?
struct MyStruct
{
union
{
double& x;
double* x_ptr;
};
MyStruct(double* ptr) : x_ptr(ptr) {}
MyStruct(double& ref) : x(ref) {}
void Repoint(double& new_ref)
{
x_ptr =3D &new_ref;
}
void Repoint(double* new_ptr)
{
x_ptr =3D new_ptr;
}
void AddOne() { x++; }
void MulTwo() { x*=3D2; }
};
--=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/94cd8c5a-41b5-450f-b177-343500c52519%40isocpp.or=
g.
------=_Part_3817_1270612616.1470068527541--
.
Author: "D. B." <db0451@gmail.com>
Date: Mon, 1 Aug 2016 17:29:09 +0100
Raw View
--089e01419d928b9e64053905198d
Content-Type: text/plain; charset=UTF-8
Union members must have the same address. A reference is an alias to an
object and hence has no address of its own The end.
--
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/CACGiwhHhOYNyU%2BfcCS0C1JNFY7JniZsyODPqZdYghFM5v3DGnA%40mail.gmail.com.
--089e01419d928b9e64053905198d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><div>Union members must have the same address. A refe=
rence is an alias to an object and hence has no address of its own The end.=
<br></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/CACGiwhHhOYNyU%2BfcCS0C1JNFY7JniZsyOD=
PqZdYghFM5v3DGnA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhHhOYNyU%=
2BfcCS0C1JNFY7JniZsyODPqZdYghFM5v3DGnA%40mail.gmail.com</a>.<br />
--089e01419d928b9e64053905198d--
.
Author: Antonio Perez <antonio@perezexcelsior.com>
Date: Mon, 1 Aug 2016 09:47:42 -0700 (PDT)
Raw View
------=_Part_5118_1287685869.1470070062699
Content-Type: multipart/alternative;
boundary="----=_Part_5119_229768456.1470070062699"
------=_Part_5119_229768456.1470070062699
Content-Type: text/plain; charset=UTF-8
Then how is a structure capable of storing a reference, if not through a
pointer with it's own address? The below code will compile without issue,
and will behave as expected:
struct MyStruct {
union { struct { double& x; }; double* x_ptr; };
};
On Monday, August 1, 2016 at 10:29:11 AM UTC-6, D. B. wrote:
>
> Union members must have the same address. A reference is an alias to an
> object and hence has no address of its own The end.
>
--
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/4d93110a-c7d9-4703-8ee3-7e1040280375%40isocpp.org.
------=_Part_5119_229768456.1470070062699
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Then how is a structure capable of storing a reference, if=
not through a pointer with it's own address? The below code will compi=
le without issue, and will behave as expected:<br><br>struct MyStruct {<br>=
=C2=A0=C2=A0=C2=A0 union { struct { double& x; }; double* x_ptr; }; <br=
>};<br><br>On Monday, August 1, 2016 at 10:29:11 AM UTC-6, D. B. wrote:<blo=
ckquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-=
left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><div>Union m=
embers must have the same address. A reference is an alias to an object and=
hence has no address of its own The end.<br></div></div></div>
</blockquote></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/4d93110a-c7d9-4703-8ee3-7e1040280375%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/4d93110a-c7d9-4703-8ee3-7e1040280375=
%40isocpp.org</a>.<br />
------=_Part_5119_229768456.1470070062699--
------=_Part_5118_1287685869.1470070062699--
.
Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 1 Aug 2016 10:11:14 -0700 (PDT)
Raw View
------=_Part_1111_1131794674.1470071474169
Content-Type: multipart/alternative;
boundary="----=_Part_1112_760691398.1470071474169"
------=_Part_1112_760691398.1470071474169
Content-Type: text/plain; charset=UTF-8
On Monday, August 1, 2016 at 12:47:42 PM UTC-4, Antonio Perez wrote:
>
> Then how is a structure capable of storing a reference, if not through a
> pointer with it's own address? The below code will compile without issue,
> and will behave as expected:
>
> struct MyStruct {
> union { struct { double& x; }; double* x_ptr; };
> };
>
What does "behave as expected" mean? Because if "as expected" means
something like this:
double val = 5.4;
MyStruct s{val};
*s.x_ptr;
That's *expressly forbidden* (as UB). `double` is not layout compatible
with the anonymous struct. And the anonymous struct is not a standard
layout type, so the common-initial-sequence rule wouldn't work either.
Unions are not type-punning boxes.
--
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/8ec4353f-7305-416f-9ee6-863bbb2302ee%40isocpp.org.
------=_Part_1112_760691398.1470071474169
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>On Monday, August 1, 2016 at 12:47:42 PM UTC-4, An=
tonio Perez wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;marg=
in-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"=
ltr">Then how is a structure capable of storing a reference, if not through=
a pointer with it's own address? The below code will compile without i=
ssue, and will behave as expected:<br><br>struct MyStruct {<br>=C2=A0=C2=A0=
=C2=A0 union { struct { double& x; }; double* x_ptr; }; <br>};<br></div=
></blockquote><div><br>What does "behave as expected" mean? Becau=
se if "as expected" means something like this:<br><br><div class=
=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-colo=
r: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap: b=
reak-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span=
style=3D"color: #008;" class=3D"styled-by-prettify">double</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> val </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" c=
lass=3D"styled-by-prettify">5.4</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span><span style=3D"color: #606;" class=3D"styled-by-pret=
tify">MyStruct</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> s</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">val</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">};</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">*</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">s</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify">x_ptr</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">;</span></div></code></div><br>That's <i>expressly forbidden<=
/i> (as UB). `double` is not layout compatible with the anonymous struct. A=
nd the anonymous struct is not a standard layout type, so the common-initia=
l-sequence rule wouldn't work either.<br><br>Unions are not type-punnin=
g boxes.<br></div><br></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/8ec4353f-7305-416f-9ee6-863bbb2302ee%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8ec4353f-7305-416f-9ee6-863bbb2302ee=
%40isocpp.org</a>.<br />
------=_Part_1112_760691398.1470071474169--
------=_Part_1111_1131794674.1470071474169--
.
Author: "D. B." <db0451@gmail.com>
Date: Mon, 1 Aug 2016 19:01:28 +0100
Raw View
--001a114ba390bd8568053906637c
Content-Type: text/plain; charset=UTF-8
On Mon, Aug 1, 2016 at 5:47 PM, Antonio Perez <antonio@perezexcelsior.com>
wrote:
> Then how is a structure capable of storing a reference, if not through a
> pointer with it's own address? The below code will compile without issue,
> and will behave as expected:
>
> struct MyStruct {
> union { struct { double& x; }; double* x_ptr; };
> };
>
I used to wonder the same thing, mostly in relation to the restriction on
storing references in arrays - but then realised how obvious it is.
The answer is: Because the struct provides a disambiguating mechanism. You
can specify whether you want the address of the referred object, or the
struct that just happens to contain the reference.
Note: not the reference itself, as it can't have an address.
anyway, the fact it's implemented as a pointer in extant compilers is
incidental and not a point in favour of any proposal.
--
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/CACGiwhHoJrE%3DAWvfYbZtG1OdUSPENnuwFSEz2KeYqkVrzvETrw%40mail.gmail.com.
--001a114ba390bd8568053906637c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On M=
on, Aug 1, 2016 at 5:47 PM, Antonio Perez <span dir=3D"ltr"><<a href=3D"=
mailto:antonio@perezexcelsior.com" target=3D"_blank">antonio@perezexcelsior=
..com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"ma=
rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"lt=
r">Then how is a structure capable of storing a reference, if not through a=
pointer with it's own address? The below code will compile without iss=
ue, and will behave as expected:<br><br>struct MyStruct {<br>=C2=A0=C2=A0=
=C2=A0 union { struct { double& x; }; double* x_ptr; }; <br>};<span cla=
ss=3D""></span><br></div></blockquote><div><br></div><div>I used to wonder =
the same thing, mostly in relation to the restriction on storing references=
in arrays - but then realised how obvious it is.<br><br>The answer is: Bec=
ause the struct provides a disambiguating mechanism. You can specify whethe=
r you want the address of the referred object, or the struct that just happ=
ens to contain the reference.<br><br>Note: not the reference itself, as=C2=
=A0 it can't have an address.<br><br></div><div>anyway, the fact it'=
;s implemented as a pointer in extant compilers is incidental and not a poi=
nt in favour of any proposal.<br></div></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/CACGiwhHoJrE%3DAWvfYbZtG1OdUSPENnuwFS=
Ez2KeYqkVrzvETrw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhHoJrE%3D=
AWvfYbZtG1OdUSPENnuwFSEz2KeYqkVrzvETrw%40mail.gmail.com</a>.<br />
--001a114ba390bd8568053906637c--
.
Author: Antonio Perez <antonio@perezexcelsior.com>
Date: Mon, 1 Aug 2016 11:42:14 -0700 (PDT)
Raw View
------=_Part_5133_1486228497.1470076935062
Content-Type: multipart/alternative;
boundary="----=_Part_5134_1769486876.1470076935070"
------=_Part_5134_1769486876.1470076935070
Content-Type: text/plain; charset=UTF-8
I believe that any time a reference is used as a member of a structure or a
class, it's implemented as a pointer. What else could it be implemented as?
I'm just asking that this implementation be extended to unions: references
stored within unions are implemented as pointers as part of the standard.
It wouldn't change the behavior of any existing code, and it would only
require minor modification to current compilers.
On Monday, August 1, 2016 at 12:01:31 PM UTC-6, D. B. wrote:
>
> On Mon, Aug 1, 2016 at 5:47 PM, Antonio Perez <ant...@perezexcelsior.com
> <javascript:>> wrote:
>
>> Then how is a structure capable of storing a reference, if not through a
>> pointer with it's own address? The below code will compile without issue,
>> and will behave as expected:
>>
>> struct MyStruct {
>> union { struct { double& x; }; double* x_ptr; };
>> };
>>
>
> I used to wonder the same thing, mostly in relation to the restriction on
> storing references in arrays - but then realised how obvious it is.
>
> The answer is: Because the struct provides a disambiguating mechanism. You
> can specify whether you want the address of the referred object, or the
> struct that just happens to contain the reference.
>
> Note: not the reference itself, as it can't have an address.
>
> anyway, the fact it's implemented as a pointer in extant compilers is
> incidental and not a point in favour of any proposal.
>
--
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/88b03e81-e02b-44df-bdee-c77db25ef663%40isocpp.org.
------=_Part_5134_1769486876.1470076935070
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I believe that any time a reference is used as a member of=
a structure or a class, it's implemented as a pointer. What else could=
it be implemented as? I'm just asking that this implementation be exte=
nded to unions: references stored within unions are implemented as pointers=
as part of the standard. It wouldn't change the behavior of any existi=
ng code, and it would only require minor modification to current compilers.=
<br><br>On Monday, August 1, 2016 at 12:01:31 PM UTC-6, D. B. wrote:<blockq=
uote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-lef=
t: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><div class=3D"g=
mail_quote">On Mon, Aug 1, 2016 at 5:47 PM, Antonio Perez <span dir=3D"ltr"=
><<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"KnX=
xy1u4BgAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:'=
;;return true;" onclick=3D"this.href=3D'javascript:';return true;">=
ant...@perezexcelsior.com</a>></span> wrote:<br><blockquote class=3D"gma=
il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div dir=3D"ltr">Then how is a structure capable of storing a refere=
nce, if not through a pointer with it's own address? The below code wil=
l compile without issue, and will behave as expected:<br><br>struct MyStruc=
t {<br>=C2=A0=C2=A0=C2=A0 union { struct { double& x; }; double* x_ptr;=
}; <br>};<span></span><br></div></blockquote><div><br></div><div>I used to=
wonder the same thing, mostly in relation to the restriction on storing re=
ferences in arrays - but then realised how obvious it is.<br><br>The answer=
is: Because the struct provides a disambiguating mechanism. You can specif=
y whether you want the address of the referred object, or the struct that j=
ust happens to contain the reference.<br><br>Note: not the reference itself=
, as=C2=A0 it can't have an address.<br><br></div><div>anyway, the fact=
it's implemented as a pointer in extant compilers is incidental and no=
t a point in favour of any proposal.<br></div></div></div></div>
</blockquote></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/88b03e81-e02b-44df-bdee-c77db25ef663%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/88b03e81-e02b-44df-bdee-c77db25ef663=
%40isocpp.org</a>.<br />
------=_Part_5134_1769486876.1470076935070--
------=_Part_5133_1486228497.1470076935062--
.
Author: "D. B." <db0451@gmail.com>
Date: Mon, 1 Aug 2016 19:47:56 +0100
Raw View
--089e0122ea38e2713d05390709d2
Content-Type: text/plain; charset=UTF-8
On Mon, Aug 1, 2016 at 7:42 PM, Antonio Perez <antonio@perezexcelsior.com>
wrote:
> I believe that any time a reference is used as a member of a structure or
> a class, it's implemented as a pointer. What else could it be implemented
> as? I'm just asking that this implementation be extended to unions:
> references stored within unions are implemented as pointers as part of the
> standard. It wouldn't change the behavior of any existing code, and it
> would only require minor modification to current compilers.
>
How a reference is implemented - in completely different situations, mind
you - is just that: an* implementation detail.* That is already not an
official detail or allowance by the Standard. There's* nothing there to
"extend".*
Your proposal cannot work for so many reasons, the most crucial being that
you can't take the address of a reference. THAT is Standardised.
Also required is that unions' names and all of their members must have the
same address.
Combining these fundamental rules of the language - this can't work, and
can't be made to work, pure and simple.
Implementing this would require breaking so many other things, and what is
the rationale?
Your original code? You realise it's pure UB, right? You're type-punning
through a union, which is explicitly disallowed.
Thankfully the Committee aren't going to do anything to encourage that.
--
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/CACGiwhH7sOAEFWdN4%3D35yb0hyYXZ3V19e%3DE18Fo-%2BBWxiF6Jhw%40mail.gmail.com.
--089e0122ea38e2713d05390709d2
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On M=
on, Aug 1, 2016 at 7:42 PM, Antonio Perez <span dir=3D"ltr"><<a href=3D"=
mailto:antonio@perezexcelsior.com" target=3D"_blank">antonio@perezexcelsior=
..com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"ma=
rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>I believe=
that any time a reference is used as a member of a structure or a class, i=
t's implemented as a pointer. What else could it be implemented as? I&#=
39;m just asking that this implementation be extended to unions: references=
stored within unions are implemented as pointers as part of the standard. =
It wouldn't change the behavior of any existing code, and it would only=
require minor modification to current compilers.</div></blockquote><div><b=
r></div><div>How a reference is implemented - in completely different situa=
tions, mind you - is just that: an<b> implementation detail.</b> That is al=
ready not an official detail or allowance by the Standard. There's<i> n=
othing there to "extend".</i><br><br></div><div>Your proposal can=
not work for so many reasons, the most crucial being that you can't tak=
e the address of a reference. THAT is Standardised.<br>Also required is tha=
t unions' names and all of their members must have the same address.<br=
>Combining these fundamental rules of the language - this can't work, a=
nd can't be made to work, pure and simple.<br><br></div><div>Implementi=
ng this would require breaking so many other things, and what is the ration=
ale?<br></div><div>=C2=A0Your original code? You realise it's pure UB, =
right? You're type-punning through a union, which is explicitly disallo=
wed.<br></div><div>Thankfully the Committee aren't going to do anything=
to encourage that.<br><br></div></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/CACGiwhH7sOAEFWdN4%3D35yb0hyYXZ3V19e%=
3DE18Fo-%2BBWxiF6Jhw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhH7sO=
AEFWdN4%3D35yb0hyYXZ3V19e%3DE18Fo-%2BBWxiF6Jhw%40mail.gmail.com</a>.<br />
--089e0122ea38e2713d05390709d2--
.
Author: "D. B." <db0451@gmail.com>
Date: Mon, 1 Aug 2016 19:50:38 +0100
Raw View
--001a114cc2a08c9ace0539071382
Content-Type: text/plain; charset=UTF-8
I mean
On Mon, Aug 1, 2016 at 5:22 PM, Antonio Perez <antonio@perezexcelsior.com>
wrote:
>
> struct MyStruct
> {
> union
> {
> double& x;
> double* x_ptr;
> };
> MyStruct(double* ptr) : x_ptr(ptr) {}
> MyStruct(double& ref) : x(ref) {}
> void Repoint(double& new_ref)
> {
> x_ptr = &new_ref;
> }
> void Repoint(double* new_ptr)
> {
> x_ptr = new_ptr;
> }
> void AddOne() { x++; }
> void MulTwo() { x*=2; }
> };
What even is the point of this? It's unadulterated UB, and for what? To get
reference-like semantics from a stored pointer? Have you ever heard of
std::reference_wrapper? That does this without blatantly flouting the
Standard for no reason and asking for your program to break horribly.
--
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/CACGiwhF60p6Fzoj5Dm_j35_r%3DAqDt17ERE0qtujLOZ_HS-yFVg%40mail.gmail.com.
--001a114cc2a08c9ace0539071382
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I mean<br><br><div class=3D"gmail_extra"><div class=3D"gma=
il_quote">On Mon, Aug 1, 2016 at 5:22 PM, Antonio Perez <span dir=3D"ltr">&=
lt;<a href=3D"mailto:antonio@perezexcelsior.com" target=3D"_blank">antonio@=
perezexcelsior.com</a>></span> wrote:<br><blockquote class=3D"gmail_quot=
e" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">=
<br>
struct MyStruct<br>
{<br>
=C2=A0 =C2=A0 union<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 double& x;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 double* x_ptr;<br>
=C2=A0 =C2=A0 };<br>
=C2=A0 =C2=A0 MyStruct(double* ptr) : x_ptr(ptr) {}<br>
=C2=A0 =C2=A0 MyStruct(double& ref) : x(ref) {}<br>
=C2=A0 =C2=A0 void Repoint(double& new_ref)<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 x_ptr =3D &new_ref;<br>
=C2=A0 =C2=A0 }<br>
=C2=A0 =C2=A0 void Repoint(double* new_ptr)<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 x_ptr =3D new_ptr;<br>
=C2=A0 =C2=A0 }<br>
=C2=A0 =C2=A0 void AddOne() { x++; }<br>
=C2=A0 =C2=A0 void MulTwo() { x*=3D2; }<br>
};</blockquote><div><br></div><div>What even is the point of this? It's=
unadulterated UB, and for what? To get reference-like semantics from a sto=
red pointer? Have you ever heard of std::reference_wrapper? That does this =
without blatantly flouting the Standard for no reason and asking for your p=
rogram to break horribly.<br>=C2=A0<br></div></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/CACGiwhF60p6Fzoj5Dm_j35_r%3DAqDt17ERE=
0qtujLOZ_HS-yFVg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhF60p6Fzo=
j5Dm_j35_r%3DAqDt17ERE0qtujLOZ_HS-yFVg%40mail.gmail.com</a>.<br />
--001a114cc2a08c9ace0539071382--
.
Author: Antonio Perez <antonio@perezexcelsior.com>
Date: Mon, 1 Aug 2016 11:51:48 -0700 (PDT)
Raw View
------=_Part_4_525419657.1470077508168
Content-Type: multipart/alternative;
boundary="----=_Part_5_93780622.1470077508168"
------=_Part_5_93780622.1470077508168
Content-Type: text/plain; charset=UTF-8
Thank you for pointing out that issue with using anonymous structs within
an anonymous union. Linked is some code that will work as you expect
<kbd>[live
example](http://coliru.stacked-crooked.com/a/56080ce580459372)</kbd>
On Monday, August 1, 2016 at 11:11:14 AM UTC-6, Nicol Bolas wrote:
>
>
>
> On Monday, August 1, 2016 at 12:47:42 PM UTC-4, Antonio Perez wrote:
>>
>> Then how is a structure capable of storing a reference, if not through a
>> pointer with it's own address? The below code will compile without issue,
>> and will behave as expected:
>>
>> struct MyStruct {
>> union { struct { double& x; }; double* x_ptr; };
>> };
>>
>
> What does "behave as expected" mean? Because if "as expected" means
> something like this:
>
> double val = 5.4;
> MyStruct s{val};
> *s.x_ptr;
>
> That's *expressly forbidden* (as UB). `double` is not layout compatible
> with the anonymous struct. And the anonymous struct is not a standard
> layout type, so the common-initial-sequence rule wouldn't work either.
>
> Unions are not type-punning boxes.
>
>
--
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/5edd42e7-6248-488b-b284-7acb6f17b4e1%40isocpp.org.
------=_Part_5_93780622.1470077508168
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Thank you for pointing out that issue with using anonymous=
structs within an anonymous union. Linked is some code that will work as y=
ou expect <br><br><kbd>[live example](http://coliru.stacked-crooked.c=
om/a/56080ce580459372)</kbd><br><br>On Monday, August 1, 2016 at 11:1=
1:14 AM UTC-6, Nicol Bolas wrote:<blockquote class=3D"gmail_quote" style=3D=
"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex=
;"><div dir=3D"ltr"><br><br>On Monday, August 1, 2016 at 12:47:42 PM UTC-4,=
Antonio Perez wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;ma=
rgin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"lt=
r">Then how is a structure capable of storing a reference, if not through a=
pointer with it's own address? The below code will compile without iss=
ue, and will behave as expected:<br><br>struct MyStruct {<br>=C2=A0=C2=A0=
=C2=A0 union { struct { double& x; }; double* x_ptr; }; <br>};<br></div=
></blockquote><div><br>What does "behave as expected" mean? Becau=
se if "as expected" means something like this:<br><br><div style=
=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-=
style:solid;border-width:1px;word-wrap:break-word"><code><div><span style=
=3D"color:#008">double</span><span style=3D"color:#000"> val </span><span s=
tyle=3D"color:#660">=3D</span><span style=3D"color:#000"> </span><span styl=
e=3D"color:#066">5.4</span><span style=3D"color:#660">;</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#606">MyStruct</span><span =
style=3D"color:#000"> s</span><span style=3D"color:#660">{</span><span styl=
e=3D"color:#000">val</span><span style=3D"color:#660">};</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#660">*</span><span style=
=3D"color:#000">s</span><span style=3D"color:#660">.</span><span style=3D"c=
olor:#000">x_ptr</span><span style=3D"color:#660">;</span></div></code></di=
v><br>That's <i>expressly forbidden</i> (as UB). `double` is not layout=
compatible with the anonymous struct. And the anonymous struct is not a st=
andard layout type, so the common-initial-sequence rule wouldn't work e=
ither.<br><br>Unions are not type-punning boxes.<br></div><br></div></block=
quote></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/5edd42e7-6248-488b-b284-7acb6f17b4e1%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5edd42e7-6248-488b-b284-7acb6f17b4e1=
%40isocpp.org</a>.<br />
------=_Part_5_93780622.1470077508168--
------=_Part_4_525419657.1470077508168--
.
Author: "D. B." <db0451@gmail.com>
Date: Mon, 1 Aug 2016 19:54:38 +0100
Raw View
--089e01419d92dea97d0539072179
Content-Type: text/plain; charset=UTF-8
That's horrible UB too. Just because g++ hasn't blown up your program yet
doesn't mean anything good. Stop this.
--
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/CACGiwhHYPe%3DbM1%3DQg-Vo24o%3D7P8kCFKLgHNJWV5E3AT93ZHZLQ%40mail.gmail.com.
--089e01419d92dea97d0539072179
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">That's horrible UB too. Just because g++ hasn't bl=
own up your program yet doesn't mean anything good. Stop this.<br></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/CACGiwhHYPe%3DbM1%3DQg-Vo24o%3D7P8kCF=
KLgHNJWV5E3AT93ZHZLQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CACGiwhHYPe=
%3DbM1%3DQg-Vo24o%3D7P8kCFKLgHNJWV5E3AT93ZHZLQ%40mail.gmail.com</a>.<br />
--089e01419d92dea97d0539072179--
.
Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Mon, 1 Aug 2016 12:46:42 -0400
Raw View
On 2016-08-01 12:29, D. B. wrote:
> Union members must have the same address. A reference is an alias to an
> object and hence has no address of its own The end.
Huh? A reference is a pointer with "sugar". Some of that sugar happens
to be that it is illegal to take its address, but that doesn't mean it
doesn't *have* an address.
Look again at the struct in the OP's message; do you mean to tell me
that the member 'x' has no address, or (since offsetof(x)==0) that said
address is different from the address of the struct itself?
That said, the real problem I see is that a reference usually cannot be
reseated. A union normally permits the active member to be changed; it's
unclear how this would work with a reference member. (Also, the OP's
code is broken because it possibly modifies an inactive member of the
union. Yes, it might work *in practice*, but it's not conforming.)
(This is apparently a recent addition to GCC; 4.9.2 rejects it, though
6.1.1 accepts it.)
--
Matthew
--
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/nnnudi%24rd%241%40blaine.gmane.org.
.