220 15996 <a4dccbc1-16ef-49cb-927f-476416c7a369@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: const T* operator->() const
Date: Tue, 3 Feb 2015 10:40:19 -0800 (PST)
Lines: 234
Approved: news@gmane.org
Message-ID: <a4dccbc1-16ef-49cb-927f-476416c7a369@isocpp.org>
References: <56a7727d-9839-45c9-be72-a6ecb61f38c9@isocpp.org>
 <CAFk2RUbDL_BedgBP_688mThXS2FfdZP=vyBjUHatvyW5qYvGEQ@mail.gmail.com>
 <467ea911-87a1-483d-8680-22429bfd7c34@isocpp.org>
 <71a46a6b-d0b5-4af7-b89c-fd67b505d22f@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_5159_94414491.1422988819657"
X-Trace: ger.gmane.org 1422988826 15142 80.91.229.3 (3 Feb 2015 18:40:26 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 3 Feb 2015 18:40:26 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBFFMYSTAKGQEJJC442Y@isocpp.org Tue Feb 03 19:40:22 2015
Return-path: <std-proposals+bncBCEKFTV6ZUMBBFFMYSTAKGQEJJC442Y@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f71.google.com ([209.85.220.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBFFMYSTAKGQEJJC442Y@isocpp.org>)
	id 1YIiOc-00029B-6q
	for gclcip-std-proposals@m.gmane.org; Tue, 03 Feb 2015 19:40:22 +0100
Original-Received: by mail-pa0-f71.google.com with SMTP id kq14sf534466332pab.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 03 Feb 2015 10:40:21 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :content-type:x-original-sender:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=36TXrIz8qsNWddc5ehjynRd9SxHOvoLaaXaawo08FLA=;
        b=oVWa9DCfSSeObXXmSNdjiVbiwwcUpMxaevsSFti71NYA7Jhcib2fwGNmNLt/Co6ssk
         BBnj4kPfoP+uI7MovrLbqJZbIJqk6oQNCU4YbJJiyI1Ng4nkf2zf/eSkjuFH4Kyv5k8z
         50K1BMJPZ+/agiqLELzU4WjXVukFW/+5GcecCokdoyxJAnFJsdHFQXzXJfXtfyQm+p0A
         2JUER8ANI0x8dayAv9PvMFzKSApZbKz5DCu6FkvOvJuEsDZWczkkPfKIBIiMzZbhxu0b
         Uof/dlTFDdlEI0eQ7GPGcOSr+0dwbSJEiLQ5m1CTrZzpnclNvTs+HTa8uSiXdJU0HjCZ
         7hbg==
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:content-type:x-original-sender:reply-to
         :precedence:mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=36TXrIz8qsNWddc5ehjynRd9SxHOvoLaaXaawo08FLA=;
        b=GQ5VhvcYrS+ykkq26KaL68kivYa02A1m19Bem5t2E6ZcwyNoX/g9UtkiIyoO4zwZO0
         wIQ4NChM/TwlX/ZCeijEU7owco5kI0Uosluj39VX1rpCBBuxcETi1KP2JxnG0w5iQ2Kz
         iwAqoJyDOxOmJtCf67sxztxr9aiTVY2/bFKzbpoSMRqfQ+uI4CP8O37+aBqsfbAUmWUM
         I96QRVy2PERg2b0Kz3B9TniXZO4PRdUGNciS66xIY+twcGyMZFo/wDplgy5CBUnAP9QD
         deziIYC+6mvHcy878QrzGKD87XYjC0TNi+xUhiEqWOx1aQ5/iPQZDhjySHDnevtke34B
         DE/g==
X-Gm-Message-State: ALoCoQmPupAnxzSgQttGIQqzAWXTEbIgXtdptiuuwi4LPErLWyV8RuKD1uLxSKXV/eOx0qzzHhzH
X-Received: by 10.66.150.33 with SMTP id uf1mr23112600pab.33.1422988821104;
        Tue, 03 Feb 2015 10:40:21 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.93.165 with SMTP id d34ls3283059qge.61.gmail; Tue, 03 Feb
 2015 10:40:20 -0800 (PST)
X-Received: by 10.140.106.8 with SMTP id d8mr313370qgf.7.1422988820125;
        Tue, 03 Feb 2015 10:40:20 -0800 (PST)
In-Reply-To: <71a46a6b-d0b5-4af7-b89c-fd67b505d22f@isocpp.org>
X-Original-Sender: jmckesson@gmail.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:15996
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/15996>

------=_Part_5159_94414491.1422988819657
Content-Type: multipart/alternative; 
	boundary="----=_Part_5160_1981418751.1422988819657"

------=_Part_5160_1981418751.1422988819657
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



On Monday, February 2, 2015 at 5:58:36 PM UTC-5, Arthur O'Dwyer wrote:
>
>
>
> On Monday, February 2, 2015 at 8:32:43 AM UTC-8, mobiphil wrote:
>>
>> On Monday, February 2, 2015 at 3:39:49 PM UTC+1, Ville Voutilainen wrote=
:
>>>
>>> On 2 February 2015 at 16:27, mobiphil <mo...@mobiphil.com> wrote:=20
>>> > Problem: compiler does not paste const operator -> when used to refer=
=20
>>> to a=20
>>> > const method=20
>>> > Proposal: well, fix it=20
>>> >=20
>>> > Context: Thought a trivial solution for cow, but does not work. The=
=20
>>> idea was=20
>>> > to have a Cow wrapper template around the classes used in my system.=
=20
>>> The=20
>>> > protocol would be: if a non const method is called on the object then=
=20
>>> a copy=20
>>> > is made (if necessary), otherwise just call the method. The idea was=
=20
>>> to play=20
>>> > with pair of const and nonconst operator->=20
>>> >=20
>>> > So, in the example below, I thought for a moment that when writing=20
>>> > a->constFunc() for non const object the const "->" operator would be=
=20
>>> chosen=20
>>> > by the compiler as constFunc() is const, but had to conclude that it=
=20
>>> is not=20
>>> > the case.
>>
>>
> Indeed, your expectation was simply wrong.
> Consider:
>
> class Foo {
>     void member();
>     void member() const;
>     void member() volatile;
> };
>
> Foo foo; foo->member();  // calls the non-`const` version
> const Foo cfoo; cfoo->member();  // calls the `const` version
> volatile Foo vfoo; vfoo->member();  // calls the `volatile` version
>
> Furthermore, you've forgotten that it's possible to overload operator->=
=20
> (possibly with const and non-const versions) so that you don't even know=
=20
> what context to look up member in, until you've resolved which operator->=
=20
> is being called.
>
> class Widget {
>     Foo* operator-> () const;
>     void member();
> };
>
> Widget w; w->member();  // calls Widget::member()
> const Widget cw; cw->member();  // calls Widget::operator->() followed by=
=20
> Foo::member()
>
> HTH,
> =E2=80=93Arthur
>

Um, I'm fairly sure that's not true <http://ideone.com/e.js/qNI9Ky>. You=20
can call const member functions from a non-const object. It's the other way=
=20
around that doesn't work. So the first one will call Foo::member, just like=
=20
the second.

And even if you did it the right way around, it would be a *compile error*.=
=20
Due to attempting to call a non-const member function on a const object.

--=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_5160_1981418751.1422988819657
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Monday, February 2, 2015 at 5:58:36 PM UTC-5, A=
rthur O'Dwyer 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"ltr"><br><br>On Monday, February 2, 2015 at 8:32:43 AM UTC-8, mobiphil =
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">On Monday, F=
ebruary 2, 2015 at 3:39:49 PM UTC+1, Ville Voutilainen wrote:<blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #cc=
c solid;padding-left:1ex">On 2 February 2015 at 16:27, mobiphil &lt;<a rel=
=3D"nofollow">mo...@mobiphil.com</a>&gt; wrote:
<br>&gt; Problem: compiler does not paste const operator -&gt; when used to=
 refer to a
<br>&gt; const method
<br>&gt; Proposal: well, fix it
<br>&gt;
<br>&gt; Context: Thought a trivial solution for cow, but does not work. Th=
e idea was
<br>&gt; to have a Cow wrapper template around the classes used in my syste=
m. The
<br>&gt; protocol would be: if a non const method is called on the object t=
hen a copy
<br>&gt; is made (if necessary), otherwise just call the method. The idea w=
as to play
<br>&gt; with pair of const and nonconst operator-&gt;
<br>&gt;
<br>&gt; So, in the example below, I thought for a moment that when writing
<br>&gt; a-&gt;constFunc() for non const object the const "-&gt;" operator =
would be chosen
<br>&gt; by the compiler as constFunc() is const, but had to conclude that =
it is not
<br>&gt; the case.</blockquote></div></blockquote><div><br></div><div>Indee=
d, your expectation was simply wrong.</div><div>Consider:</div><div><br></d=
iv><div style=3D"background-color:rgb(250,250,250);border:1px solid rgb(187=
,187,187);word-wrap:break-word"><code><div><span style=3D"color:#008">class=
</span><span style=3D"color:#000"> </span><span style=3D"color:#606">Foo</s=
pan><span style=3D"color:#000"> </span><span style=3D"color:#660">{</span><=
span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#00=
8">void</span><span style=3D"color:#000"> member</span><span style=3D"color=
:#660">();</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span =
style=3D"color:#008">void</span><span style=3D"color:#000"> member</span><s=
pan style=3D"color:#660">()</span><span style=3D"color:#000"> </span><span =
style=3D"color:#008">const</span><span style=3D"color:#660">;</span><span s=
tyle=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#008">voi=
d</span><span style=3D"color:#000"> member</span><span style=3D"color:#660"=
>()</span><span style=3D"color:#000"> </span><span style=3D"color:#008">vol=
atile</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"><b=
r><br></span><span style=3D"color:#606">Foo</span><span style=3D"color:#000=
"> foo</span><span style=3D"color:#660">;</span><span style=3D"color:#000">=
 foo</span><span style=3D"color:#660">-&gt;</span><span style=3D"color:#000=
">member</span><span style=3D"color:#660">();</span><span style=3D"color:#0=
00"> &nbsp;</span><span style=3D"color:#800">// calls the non-`const` versi=
on</span><span style=3D"color:#000"><br></span><span style=3D"color:#008">c=
onst</span><span style=3D"color:#000"> </span><span style=3D"color:#606">Fo=
o</span><span style=3D"color:#000"> cfoo</span><span style=3D"color:#660">;=
</span><span style=3D"color:#000"> cfoo</span><span style=3D"color:#660">-&=
gt;</span><span style=3D"color:#000">member</span><span style=3D"color:#660=
">();</span><span style=3D"color:#000"> &nbsp;</span><span style=3D"color:#=
800">// calls the `const` version</span><span style=3D"color:#000"><br></sp=
an><span style=3D"color:#008">volatile</span><span style=3D"color:#000"> </=
span><span style=3D"color:#606">Foo</span><span style=3D"color:#000"> vfoo<=
/span><span style=3D"color:#660">;</span><span style=3D"color:#000"> vfoo</=
span><span style=3D"color:#660">-&gt;</span><span style=3D"color:#000">memb=
er</span><span style=3D"color:#660">();</span><span style=3D"color:#000"> &=
nbsp;</span><span style=3D"color:#800">// calls the `volatile` version</spa=
n></div></code></div><div><br>Furthermore, you've forgotten that it's possi=
ble to overload <font face=3D"courier new, monospace">operator-&gt;</font> =
(possibly with const and non-const versions) so that you don't even know wh=
at context to look up member in, until you've resolved which <font face=3D"=
courier new, monospace">operator-&gt;</font> is being called.</div><div><br=
></div><div style=3D"background-color:rgb(250,250,250);border:1px solid rgb=
(187,187,187);word-wrap:break-word"><code><div><span style=3D"color:#008">c=
lass</span><span style=3D"color:#000"> </span><span style=3D"color:#606">Wi=
dget</span><span style=3D"color:#000"> </span><span style=3D"color:#660">{<=
/span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"co=
lor:#606">Foo</span><span style=3D"color:#660">*</span><span style=3D"color=
:#000"> </span><span style=3D"color:#008">operator</span><span style=3D"col=
or:#660">-&gt;</span><span style=3D"color:#000"> </span><span style=3D"colo=
r:#660">()</span><span style=3D"color:#000"> </span><span style=3D"color:#0=
08">const</span><span style=3D"color:#660">;</span><span style=3D"color:#00=
0"><br>&nbsp; &nbsp; </span><span style=3D"color:#008">void</span><span sty=
le=3D"color:#000"> member</span><span style=3D"color:#660">();</span><span =
style=3D"color:#000"><br></span><span style=3D"color:#660">};</span><span s=
tyle=3D"color:#000"><br><br></span><span style=3D"color:#606">Widget</span>=
<span style=3D"color:#000"> w</span><span style=3D"color:#660">;</span><spa=
n style=3D"color:#000"> w</span><span style=3D"color:#660">-&gt;</span><spa=
n style=3D"color:#000">member</span><span style=3D"color:#660">();</span><s=
pan style=3D"color:#000"> &nbsp;</span><span style=3D"color:#800">// calls =
Widget::member()</span><span style=3D"color:#000"><br></span><span style=3D=
"color:#008">const</span><span style=3D"color:#000"> </span><span style=3D"=
color:#606">Widget</span><span style=3D"color:#000"> cw</span><span style=
=3D"color:#660">;</span><span style=3D"color:#000"> cw</span><span style=3D=
"color:#660">-&gt;</span><span style=3D"color:#000">member</span><span styl=
e=3D"color:#660">();</span><span style=3D"color:#000"> &nbsp;</span><span s=
tyle=3D"color:#800">// calls Widget::operator-&gt;() followed by Foo::membe=
r()</span><span style=3D"color:#000"><br></span></div></code></div><div><br=
></div><div>HTH,</div><div>=E2=80=93Arthur<br></div></div></blockquote><div=
><br>Um, I'm <a href=3D"http://ideone.com/e.js/qNI9Ky">fairly sure that's n=
ot true</a>. You can call const member functions from a non-const object. I=
t's the other way around that doesn't work. So the first one will call Foo:=
:member, just like the second.<br><br>And even if you did it the right way =
around, it would be a <i>compile error</i>. Due to attempting to call a non=
-const member function on a const object.<br></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_5160_1981418751.1422988819657--
------=_Part_5159_94414491.1422988819657--

.
