220 12715 <13fd60e5-2882-48bc-a9df-3c8d38b416eb@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Bengt Gustafsson <bengt.gustafsson@beamways.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Usability extensions to offsetof
Date: Sun, 7 Sep 2014 15:57:26 -0700 (PDT)
Lines: 266
Approved: news@gmane.org
Message-ID: <13fd60e5-2882-48bc-a9df-3c8d38b416eb@isocpp.org>
References: <fc4c748a-1805-4e92-ae3f-58bbfe6b7537@isocpp.org>
 <1790122.GP859xiMW3@tjmaciei-mobl4>
 <dedaddc6-c382-4256-83a9-6af4dbd91caf@isocpp.org>
 <3079502.NAEi170Di4@tjmaciei-mobl4>
 <c11dc8b4-df55-4530-9322-b6bd5de9ce54@isocpp.org>
 <07066304-6357-4a10-824b-7c0451608996@isocpp.org>
 <CAOfiQq=pVvepG1ciqtxhHpa_FTqJGyimbyZ+9ihWZmJCT0oRCA@mail.gmail.com>
 <CAOfiQqmK6Zq=Yb7auaNFjrwCR=HkTc6bYVcV54z26mz5FxPKTQ@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_518_1169443003.1410130646690"
X-Trace: ger.gmane.org 1410130658 28792 80.91.229.3 (7 Sep 2014 22:57:38 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sun, 7 Sep 2014 22:57:38 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCRIRSPDTQIRBV6FWOQAKGQENNRSEHI@isocpp.org Mon Sep 08 00:57:30 2014
Return-path: <std-proposals+bncBCRIRSPDTQIRBV6FWOQAKGQENNRSEHI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pd0-f200.google.com ([209.85.192.200])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCRIRSPDTQIRBV6FWOQAKGQENNRSEHI@isocpp.org>)
	id 1XQlOj-0006ig-9c
	for gclcip-std-proposals@m.gmane.org; Mon, 08 Sep 2014 00:57:29 +0200
Original-Received: by mail-pd0-f200.google.com with SMTP id ft15sf11383074pdb.7
        for <gclcip-std-proposals@m.gmane.org>; Sun, 07 Sep 2014 15:57:27 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=W5eFtMA8f8xo1fuuSwwFN+QO2WB9K+77MvqaAIRU+RI=;
        b=j9L56wIojw1+36UjMzSSkhdPMh1ckdGf63T/fjOVdbErMyDdvFcyBgPx2KE1aQ92mW
         n3gvis83QLwKzf2XbVSZtEZ1x1141FhXSLH+5AywfvKJ4j0kpuLhr3Wwr7hNkDq/nr8u
         rqGvR6yhJP4EPRAnFhgyZbiWw9TLlcNHkob7pIMRf4RcTC5+ityXAMUem19TzjRns4a5
         Wzachs0sTDYJV9Fv1Gn17I2dfjvjDlZY2iYQfTO7bjpoV2HuvdECIveQ7fTIWpSKAMJn
         RBP4ifzRdJyJhSd9d/3Stoizwga/wUQVQyxCgZPKBFIMCsnr9IvkUfAprYcoyWbzvsDI
         r48Q==
X-Gm-Message-State: ALoCoQn9OnP/WBs3k6yJrIVu2sKUobCp3zgmgLKzl4E84H9jghfGriNogl0ApKeAqMzadCQNiNb6
X-Received: by 10.66.65.131 with SMTP id x3mr14319938pas.13.1410130647866;
        Sun, 07 Sep 2014 15:57:27 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.19.108 with SMTP id 99ls1541688qgg.29.gmail; Sun, 07 Sep
 2014 15:57:27 -0700 (PDT)
X-Received: by 10.140.19.213 with SMTP id 79mr564582qgh.5.1410130647131;
        Sun, 07 Sep 2014 15:57:27 -0700 (PDT)
In-Reply-To: <CAOfiQqmK6Zq=Yb7auaNFjrwCR=HkTc6bYVcV54z26mz5FxPKTQ@mail.gmail.com>
X-Original-Sender: bengt.gustafsson@beamways.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:12715
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/12715>

------=_Part_518_1169443003.1410130646690
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Inner classes: If the only instance of the inner class is by value in its=
=20
outer class then we could accomplish accessing the outer class's members=20
without any coding:

class Outer {
    int x;

    class Inner {
        void f() {
            int y =3D x; // This is assigning from Outer's x.
        }
    } m_inner;
};

This is why I ment by "starting out with this particular case", i.e. that=
=20
it would be nice if this worked. Of course, once you start creating=20
instances of Inner that are not by value you'd have to
handle the hidden pointer issues, which is the sticky part, I assume. It=20
would be possible to state in the standard that "nested classes can access=
=20
outer class' members if the only instances are by value in the outer=20
class." which would make code above legal.

I understand that this is creating another special case so it would be of=
=20
course even better if full inner class functionality was implemented as it=
=20
has a wider spectrum of uses. Even this limited possibility would be very=
=20
useful though as it is one of the more common patterns and furthermore
the only pattern where the inner class would perform better than a=20
non-inner class with an explicit "outer" pointer sent to the constructor.=
=20
This performance gain is what  CONTAINING_RECORD, offsetof and -PMV is=20
trying to reach at, resulting in rather ugly code in all cases.

One problem would be that the compiler would have to check that no illegal=
=20
instances are created. One simple trick would be to mandate that such=20
classes are private but I think that it should be possible to do the check=
=20
while parsing the class head. Well, if the code bodies were in a cpp file=
=20
this would not be possible, so a naive
implementation would only get a link error (providing the use of x creates=
=20
a name mangling change). This indicates that a "inner" keyword is necessary=
=20
(I hoped to get away without one). This may be just as well if an extension=
=20
to full inner classes is on the longer term agenda.


Den s=C3=B6ndagen den 7:e september 2014 kl. 01:02:42 UTC+2 skrev Richard S=
mith:
>
> On 6 Sep 2014 15:59, "Richard Smith" <ric...@metafoo.co.uk <javascript:>>=
=20
> wrote:
> >
> > On 6 Sep 2014 01:13, "Bengt Gustafsson" <bengt.gu...@beamways.com=20
> <javascript:>> wrote:
> > >
> > > To me it seems that the logical extension would be to allow casting o=
f=20
> a data member pointer to int to reveal the offset it represents. Isn't th=
is=20
> what we're trying to accomplish anyway?
> >
> > I think we're trying to capture a higher level notion than that.
> >
> > > Or is the task at hand to get the offset of a member in class A as it=
=20
> would be if refferred to via a pointer to class B where virtual and=20
> multiple inheritance complicates matters?
> > >
> > > I still think that the unary minus on a PMV is wierd as it can't be=
=20
> used "for real" only in offset calculations.
> >
> > What do you mean by "for real"? You would be able to do this:
> >
> > struct A {
> >   int n;
> > } a;
> > int *p =3D &a.n;
> > A *pa =3D p->* -&A::n; // pa =3D=3D &a
>
> Err, A *pa =3D &p->*-&A::n;
>
> > ... which is as "for real" as any existing use of member pointers, as=
=20
> far as I can see.
> >
> > > If the target for all this is the CONTAINING_RECORD use case, (which=
=20
> Itoo  have used at times to save an unnecessary indirection level),=20
> wouldn't it be more appropriate to revisit the inner classes discussion,=
=20
> maybe starting out with this particular use case. I see some parallels wi=
th=20
> lambda capture, an inner class being a little to its outer class as a=20
> lambda is to its containing function.
> >
> > I don't think that inner classes are relevant here; they're about havin=
g=20
> an independent object at an independent address that intrinsically has an=
=20
> implicit reference to its parent. This is about having a nested object th=
at=20
> is extrinsically known to be at a fixed offset within its parent and bein=
g=20
> able to navigate between the two in either direction.
> >
> > > You received this message because you are subscribed to the Google=20
> Groups "ISO C++ Standard - Future Proposals" group.
> > > To unsubscribe from this group and stop receiving emails from it, sen=
d=20
> an email to std-proposal...@isocpp.org <javascript:>.
> > > To post to this group, send email to std-pr...@isocpp.org=20
> <javascript:>.
> > > Visit this group at=20
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
> =20

--=20

---=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

------=_Part_518_1169443003.1410130646690
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Inner classes: If the only instance of the inner class is =
by value in its outer class then we could accomplish accessing the outer cl=
ass's members without any coding:<div><br></div><div>class Outer {</div><di=
v>&nbsp; &nbsp; int x;</div><div><br></div><div>&nbsp; &nbsp; class Inner {=
</div><div>&nbsp; &nbsp; &nbsp; &nbsp; void f() {</div><div>&nbsp; &nbsp; &=
nbsp; &nbsp; &nbsp; &nbsp; int y =3D x; // This is assigning from Outer's x=
..</div><div>&nbsp; &nbsp; &nbsp; &nbsp; }</div><div>&nbsp; &nbsp; } m_inner=
;</div><div><div>};</div><div><br></div><div>This is why I ment by "startin=
g out with this particular case", i.e. that it would be nice if this worked=
.. Of course, once you start creating instances of Inner that are not by val=
ue you'd have to</div><div>handle the hidden pointer issues, which is the s=
ticky part, I assume. It would be possible to state in the standard that "n=
ested classes can access outer class' members if the only instances are by =
value in the outer class." which would make code above legal.</div><div><br=
></div><div>I understand that this is creating another special case so it w=
ould be of course even better if full inner class functionality was impleme=
nted as it has a wider spectrum of uses. Even this limited possibility woul=
d be very useful though as it is one of the more common patterns and furthe=
rmore</div><div>the only pattern where the inner class would perform better=
 than a non-inner class with an explicit "outer" pointer sent to the constr=
uctor. This performance gain is what&nbsp;<span style=3D"color: rgb(80, 0, =
80); font-size: 13px;">&nbsp;CONTAINING_RECORD, offsetof and -PMV is trying=
 to reach at, resulting in rather ugly code in all cases.</span></div><div>=
<br></div><div>One problem would be that the compiler would have to check t=
hat no illegal instances are created. One simple trick would be to mandate =
that such classes are private but I think that it should be possible to do =
the check while parsing the class head. Well, if the code bodies were in a =
cpp file this would not be possible, so a naive</div><div>implementation wo=
uld only get a link error (providing the use of x creates a name mangling c=
hange). This indicates that a "inner" keyword is necessary (I hoped to get =
away without one). This may be just as well if an extension to full inner c=
lasses is on the longer term agenda.</div><div><br><br>Den s=C3=B6ndagen de=
n 7:e september 2014 kl. 01:02:42 UTC+2 skrev Richard Smith:<blockquote cla=
ss=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #=
ccc solid;padding-left: 1ex;"><p dir=3D"ltr">On 6 Sep 2014 15:59, "Richard =
Smith" &lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=
=3D"Ofv-yAogMFYJ" onmousedown=3D"this.href=3D'javascript:';return true;" on=
click=3D"this.href=3D'javascript:';return true;">ric...@metafoo.co.uk</a>&g=
t; wrote:<br>
&gt;<br>
&gt; On 6 Sep 2014 01:13, "Bengt Gustafsson" &lt;<a href=3D"javascript:" ta=
rget=3D"_blank" gdf-obfuscated-mailto=3D"Ofv-yAogMFYJ" onmousedown=3D"this.=
href=3D'javascript:';return true;" onclick=3D"this.href=3D'javascript:';ret=
urn true;">bengt.gu...@beamways.com</a><wbr>&gt; wrote:<br>
&gt; &gt;<br>
&gt; &gt; To me it seems that the logical extension would be to allow casti=
ng of a data member pointer to int to reveal the offset it represents. Isn'=
t this what we're trying to accomplish anyway?<br>
&gt;<br>
&gt; I think we're trying to capture a higher level notion than that.<br>
&gt;<br>
&gt; &gt; Or is the task at hand to get the offset of a member in class A a=
s it would be if refferred to via a pointer to class B where virtual and mu=
ltiple inheritance complicates matters?<br>
&gt; &gt;<br>
&gt; &gt; I still think that the unary minus on a PMV is wierd as it can't =
be used "for real" only in offset calculations.<br>
&gt;<br>
&gt; What do you mean by "for real"? You would be able to do this:<br>
&gt;<br>
&gt; struct A {<br>
&gt; &nbsp; int n;<br>
&gt; } a;<br>
&gt; int *p =3D &amp;a.n;<br>
&gt; A *pa =3D p-&gt;* -&amp;A::n; // pa =3D=3D &amp;a</p>
<p dir=3D"ltr">Err, A *pa =3D &amp;p-&gt;*-&amp;A::n;</p>
<p dir=3D"ltr">&gt; ... which is as "for real" as any existing use of membe=
r pointers, as far as I can see.<br>
&gt;<br>
&gt; &gt; If the target for all this is the&nbsp;CONTAINING_RECORD&nbsp;use=
 case, (which Itoo &nbsp;have used at times to save an unnecessary indirect=
ion level), wouldn't it be more appropriate to revisit the inner classes di=
scussion, maybe starting out with this particular use case. I see some para=
llels with lambda capture, an inner class being a little to its outer class=
 as a lambda is to its containing function.<br>
&gt;<br>
&gt; I don't think that inner classes are relevant here; they're about havi=
ng an independent object at an independent address that intrinsically has a=
n implicit reference to its parent. This is about having a nested object th=
at is extrinsically known to be at a fixed offset within its parent and bei=
ng able to navigate between the two in either direction.<br>
&gt;<br>
&gt; &gt; You received this message because you are subscribed to the Googl=
e Groups "ISO C++ Standard - Future Proposals" group.<br>
&gt; &gt; To unsubscribe from this group and stop receiving emails from it,=
 send an email to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-=
mailto=3D"Ofv-yAogMFYJ" onmousedown=3D"this.href=3D'javascript:';return tru=
e;" onclick=3D"this.href=3D'javascript:';return true;">std-proposal...@<wbr=
>isocpp.org</a>.<br>
&gt; &gt; To post to this group, send email to <a href=3D"javascript:" targ=
et=3D"_blank" gdf-obfuscated-mailto=3D"Ofv-yAogMFYJ" onmousedown=3D"this.hr=
ef=3D'javascript:';return true;" onclick=3D"this.href=3D'javascript:';retur=
n true;">std-pr...@isocpp.org</a>.<br>
&gt; &gt; Visit this group at <a href=3D"http://groups.google.com/a/isocpp.=
org/group/std-proposals/" target=3D"_blank" onmousedown=3D"this.href=3D'htt=
p://groups.google.com/a/isocpp.org/group/std-proposals/';return true;" oncl=
ick=3D"this.href=3D'http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/';return true;">http://groups.google.com/a/<wbr>isocpp.org/group/std-<wb=
r>proposals/</a>.<br>
</p>
</blockquote></div></div></div>

<p></p>

-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_518_1169443003.1410130646690--

.
