220 41147 <CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Phil Miller <unmobile@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Return this or *this in overridden method in
 child class?
Date: Fri, 30 Nov 2018 07:48:50 -0600
Lines: 167
Approved: news@gmane.org
Message-ID: <CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA@mail.gmail.com>
References: <CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq=n2JTkUcHHE6Qbux-PA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="000000000000db7bba057be20fc2"
X-Trace: blaine.gmane.org 1543585618 18102 195.159.176.226 (30 Nov 2018 13:46:58 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Fri, 30 Nov 2018 13:46:58 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCSLDEHDREGRBT77QTQAKGQEAH6FYMQ@isocpp.org Fri Nov 30 14:46:54 2018
Return-path: <std-proposals+bncBCSLDEHDREGRBT77QTQAKGQEAH6FYMQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lj1-f198.google.com ([209.85.208.198])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCSLDEHDREGRBT77QTQAKGQEAH6FYMQ@isocpp.org>)
	id 1gSj8A-0004by-1i
	for gclcip-std-proposals@m.gmane.org; Fri, 30 Nov 2018 14:46:54 +0100
Original-Received: by mail-lj1-f198.google.com with SMTP id s64-v6sf1762659lje.19
        for <gclcip-std-proposals@m.gmane.org>; Fri, 30 Nov 2018 05:49:05 -0800 (PST)
ARC-Seal: i=2; a=rsa-sha256; t=1543585745; cv=pass;
        d=google.com; s=arc-20160816;
        b=KCyPe+OcpSHUyjKF6zWexu/zbd/Y0wrdpWOvR93z8Y7jJ4vCt/SeG7ZtC//ES7drO/
         tD9ERYrW/6VdVQvQtaaOtQIyiPI/qHp4vjid5svxaaw5F3Ry02R7KfDOqg+dR7nlJ5Ix
         SBXGyq4nA1cg1/OzoYBIe+BRFJoRfCwt8Yh7F7vXEehbOPDZFg91RceO8VS22wNiBaSn
         29Mdsy32/ZKeWe+F3l1k0pHCsi8ZrqAhFAU3gJkk6HvjvE7N+bB43YBTIMs3ZYrx8XgX
         JF0SpnylerAg84pdj+ZdNMSusLKndA+PCf2RHVpXCbiWMpgOHkTDOBseosKPDV4Lr5zg
         hfAQ==
ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post
         :list-id:mailing-list:precedence:reply-to:to:subject:message-id:date
         :from:in-reply-to:references:mime-version:dkim-signature;
        bh=4UWePeyfEkEUIGuJU5kF1B97/4nxgqCwKpfWwCCAHtA=;
        b=hT9jZuwZpyd1nEYvDiwMsMFf+wVNcx0JY45wXTBdXeRM/pCSuHVf8VPfs/NKLGL9uO
         eUXtkBRm7KRS38SmYzdDCmCG7uJWYU8v/1Guv07N5Foh/HvR2vlZ2Kr51PxZhqIF3nAz
         6DVrQTz6c5+/rNi5tWUIZZ5WcUL144ZmkoRbCKCJ8eqVQCX5BfDb5sus+noe7284DP0q
         TPUq3ut8cO2o43JoiHecAWosQcVQc+kL+hQCAXcYlN/ZfsmaVeE2DzW7ICcs2z+ByUaV
         joIgS2qceScXp0jzQcXz55Q8AwaEdD7Kn+TKaSztW8WQvZLEAmc3zZRip9v+szceR1gn
         jBtw==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b="eS5Pve/d";
       spf=pass (google.com: domain of unmobile@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=unmobile@gmail.com;
       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=4UWePeyfEkEUIGuJU5kF1B97/4nxgqCwKpfWwCCAHtA=;
        b=lnXd4u20dUltfRbua3D95s4GJpuO/RTEyjQ5T6bnSSIMms4aNy4Rp+ZgxcE91vxNv6
         BgAaaD3t84VPDGiHZgGJs85wFA5exUo3Z1npx8t2uHweLv/a4lk7dDerMmC7kGJHBFdi
         xI9XbvqS1AmCSu+eIWP3fbfW5Clalx7aaTC3Kugeb99UHFFI9o+SUaEa9EzMrX74tGey
         tkqrsGKz+lpArZoZit4riXJTew8n/iIqnNRrBHXlk3c/t6BZWpqC0YH+UruBZYUo5hM5
         S2TOGhW8mpPhqO3vKK0rwFBF4ILgzu0GluHaACQrV8wTss6Otog7q8Cw85k/rsLw2QNw
         vZ2A==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=4UWePeyfEkEUIGuJU5kF1B97/4nxgqCwKpfWwCCAHtA=;
        b=U9UiaoCP2r4WmixxkxSNALSfiFgRbg9ScRkE/89NfjdZY/94vXTEyNJKoMlEDN0Zr3
         tZ/foxiz+Bl1D8IL3hVo6mrYHrjjqyrf847hWjhoRrB+Ja/pkjqCxwvtWpNNa/EAJlaS
         K1q14KjYM4uEI6cQYMcGklG7XpPD+7nHrGULPwGIHDk0YCX03J5eEjmb5e2R3qie6sgg
         QiTNo7czXqezlynXJidmLPnpN2eq9dCyghVRjrT55wa0bJEHs+kYTYJgNqVWXC+OTGrQ
         3+MMtPIAhPxXGZFoQgYESP6U/g8do85tEKFG0+xB6V1kAOw57FncxwL0tHR6fiU3t1ad
         LFvA==
X-Gm-Message-State: AA+aEWaj9sdNREcEMBjcMyYMcXDAyUhTVe9MywQGnzuYGqLl/9jAQDkH
	4NbhlAGTSM7nkwAbROaJb0kYow==
X-Google-Smtp-Source: AFSGD/Xy6/+IyxlyjkCYUQ4ZJKu4LBz+siF61hNP8nLCdtXEIheYg9eCkmMFinNwnZ5GZrRg9ynkDg==
X-Received: by 2002:a2e:860a:: with SMTP id a10-v6mr603562lji.26.1543585744898;
        Fri, 30 Nov 2018 05:49:04 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:a2e:5d9d:: with SMTP id v29-v6ls837453lje.12.gmail; Fri, 30
 Nov 2018 05:49:03 -0800 (PST)
X-Received: by 2002:a2e:478f:: with SMTP id u137-v6mr3889108lja.142.1543585743172;
        Fri, 30 Nov 2018 05:49:03 -0800 (PST)
ARC-Seal: i=1; a=rsa-sha256; t=1543585743; cv=none;
        d=google.com; s=arc-20160816;
        b=MjSeYC5K3zKTuxGcRX/FuIdsbE2JC3dic9keJgUbLS/Z4N4taoD06j1BJHPDh8wxR+
         xXBruUuseuneyYedrJyMJyaaaXFmO/9pJ0Cf8zCl7RX/uL5taBYdizdiXx7+iJcVnnGV
         iEoUK4HgTBqvNhqDkpJOAos0woXLVCAqXmuzf+V7EaITSyh02cm/p/vFIuradATE9Oat
         HZvAceoSBCNeJdlzdoXLVWRhYh8x7SWiXlkYczu0neJhECuQv352KiG+iLvF1p/4tkqO
         v2eD9YsRX5Y4bwdURWbYbTQ203QIOmfbdfYGMx3xapBDTaLN2/ALR17F0SZSROXKrcdQ
         TmZw==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=to:subject:message-id:date:from:in-reply-to:references:mime-version
         :dkim-signature;
        bh=GjkfE5T7RZrsjeJasAxKFapOXiuj8PW/ycZH/Y5GxrQ=;
        b=GD03TnWBHkfLhWAHPgo2bHB3hra6vgfQl1/jQ9wEWG7tbKnd2PpwiQ6SGQG5TAj4c7
         2p23kmDQkpSrW1BnUO2rQ0GkrP/RBBSSXu/5z2Q5KE+7UTmcUifqsJcGUIcT9/cyKjyI
         GlvAuzHvPD0nk+BL+s5pssoaRoQwFPgDLje59wN6qdCOe9ghPTxcepjOJwI93rIZWDXc
         M1Zf+MZ3ZnvpvNulzEYJNxY52TzezRhLivVQlxLuJI5TYNtenj/FdhUDz9K9gBI08/Hg
         zeMaITA/OY4b7qhnjx9XFbrMzyyIpej9x69q9iOR++mQ4ssH8pPSrdRZlOTCSN4+jiLQ
         kVYQ==
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b="eS5Pve/d";
       spf=pass (google.com: domain of unmobile@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=unmobile@gmail.com;
       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com
Original-Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41])
        by mx.google.com with SMTPS id a13-v6sor3558591ljj.25.2018.11.30.05.49.03
        for <std-proposals@isocpp.org>
        (Google Transport Security);
        Fri, 30 Nov 2018 05:49:03 -0800 (PST)
Received-SPF: pass (google.com: domain of unmobile@gmail.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41;
X-Received: by 2002:a2e:88cf:: with SMTP id a15-v6mr3863630ljk.76.1543585742419;
 Fri, 30 Nov 2018 05:49:02 -0800 (PST)
In-Reply-To: <CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq=n2JTkUcHHE6Qbux-PA@mail.gmail.com>
X-Original-Sender: unmobile@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com header.s=20161025 header.b="eS5Pve/d";       spf=pass
 (google.com: domain of unmobile@gmail.com designates 209.85.220.41 as
 permitted sender) smtp.mailfrom=unmobile@gmail.com;       dmarc=pass (p=NONE
 sp=QUARANTINE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://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>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:41147
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/41147>

--000000000000db7bba057be20fc2
Content-Type: text/plain; charset="UTF-8"

I believe you could accomplish what you want with CRTP. Template the base
class over its child class type, and have the base class method return a
reference to that child type. Without having tried it, I think you may need
to add a hidden extra base-of-base class to make it work, but that may be
avoided.

On Fri, Nov 30, 2018, 7:38 AM Vu Pham <phvu225@gmail.com wrote:

> While thinking about my (ahem) streaming proposal
> <https://groups.google.com/a/isocpp.org/forum/?utm_medium=email&utm_source=footer#!msg/std-proposals/w0j1pF8gQPQ/YzHdodLoBwAJ>,
> I think I found a missing feature in C++. Since this group is highly
> populated with people speaking C++ fluently, I will ask this here instead.
>
> Assume I have the following classes:
>
> class A {
> public:
>     A& f1(float& x) {
>         x++;
>         return *this;
>     }
> };
>
> class B: public A {
> public:
>     B& f2(float& x) {
>         x--;
>         return *this;
>     }
> };
>
> int main()
> {
>     float x(100.0f);
>     B b;
>     *static_cast<B>(b.f1(x)).f2(x);*   // this doesn't compile
>     return 0;
> }
>
> The idea is I want B::f1() to return an instance of B (it should, given
> the implementation of f1), but since the return type of f1() is A&,
> apparently there is no way to make it work the way I want. Here are a few
> alternatives I tried:
>
> - Make f1() returns A*, then cast the returned pointer. This doesn't look
> really nice.
> - Change the return type of f1() to something like decltype(*this) or
> auto, none of this seems to work.
>
> This issue may get worse if A and B are templated, or in a more
> complicated scenario where A::f1() returns a child class C of A, while B is
> a child of A but not C, and I want to override B::f1() to return D, which
> is a child class of B.
>
> Since C++ allows multiple inheritence, and in the same light with the
> *concept* idea that are coming up in new C++, should we coin a new
> annotation to indicate that a function returns an instance of *this*
> class? The actual returned type will change depending on the actual type of
> the class that mix those functions in.
>
> What do you think?
> Vu
>
> --
> 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/CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq%3Dn2JTkUcHHE6Qbux-PA%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq%3Dn2JTkUcHHE6Qbux-PA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA%40mail.gmail.com.

--000000000000db7bba057be20fc2
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto">I believe you could accomplish what you want with CRTP. T=
emplate the base class over its child class type, and have the base class m=
ethod return a reference to that child type. Without having tried it, I thi=
nk you may need to add a hidden extra base-of-base class to make it work, b=
ut that may be avoided.</div><br><div class=3D"gmail_quote"><div dir=3D"ltr=
">On Fri, Nov 30, 2018, 7:38 AM Vu Pham &lt;<a href=3D"mailto:phvu225@gmail=
..com">phvu225@gmail.com</a> wrote:<br></div><blockquote class=3D"gmail_quot=
e" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">=
<div dir=3D"ltr"><div dir=3D"ltr"><div>While thinking about my (ahem) <a hr=
ef=3D"https://groups.google.com/a/isocpp.org/forum/?utm_medium=3Demail&amp;=
utm_source=3Dfooter#!msg/std-proposals/w0j1pF8gQPQ/YzHdodLoBwAJ" target=3D"=
_blank" rel=3D"noreferrer">streaming proposal</a>, I think I found a missin=
g feature in C++. Since this group is highly populated with people speaking=
 C++ fluently, I will ask this here instead.</div><div><br></div><div>Assum=
e I have the following classes:</div><div><br></div><div><span style=3D"fon=
t-family:monospace,monospace">class A {<br>public:<br>=C2=A0=C2=A0=C2=A0 A&=
amp; f1(float&amp; x) {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 x++;<=
br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;<br>=C2=A0=C2=A0=
=C2=A0 }<br>};<br><br>class B: public A {<br>public:<br>=C2=A0=C2=A0=C2=A0 =
B&amp; f2(float&amp; x) {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 x--=
;<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;<br>=C2=A0=C2=
=A0=C2=A0 }<br>};<br><br>int main()<br>{<br>=C2=A0=C2=A0=C2=A0 float x(100.=
0f);<br>=C2=A0=C2=A0=C2=A0 B b;<br>=C2=A0=C2=A0=C2=A0 <b>static_cast&lt;B&g=
t;(b.f1(x)).f2(x);</b>=C2=A0=C2=A0 // this doesn&#39;t compile<br>=C2=A0=C2=
=A0=C2=A0 return 0;<br>}</span><br></div><div><br></div><div>The idea is I =
want B::f1() to return an instance of B (it should, given the implementatio=
n of f1), but since the return type of f1() is A&amp;, apparently there is =
no way to make it work the way I want. Here are a few alternatives I tried:=
</div><div><br></div><div>- Make f1() returns A*, then cast the returned po=
inter. This doesn&#39;t look really nice.</div><div>- Change the return typ=
e of f1() to something like decltype(*this) or auto, none of this seems to =
work.</div><div><br></div><div>This issue may get worse if A and B are temp=
lated, or in a more complicated scenario where A::f1() returns a child clas=
s C of A, while B is a child of A but not C, and I want to override B::f1()=
 to return D, which is a child class of B.</div><div><br></div><div>Since C=
++ allows multiple inheritence, and in the same light with the <i>concept</=
i> idea that are coming up in new C++, should we coin a new annotation to i=
ndicate that a function returns an instance of <i>this</i> class? The actua=
l returned type will change depending on the actual type of the class that =
mix those functions in.</div><div><br></div><div>What do you think?</div><d=
iv>Vu<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&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" target=3D"_=
blank" rel=3D"noreferrer">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank" rel=3D"noreferrer">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/CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq%3Dn2=
JTkUcHHE6Qbux-PA%40mail.gmail.com?utm_medium=3Demail&amp;utm_source=3Dfoote=
r" target=3D"_blank" rel=3D"noreferrer">https://groups.google.com/a/isocpp.=
org/d/msgid/std-proposals/CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq%3Dn2JTkUcHHE6Qbu=
x-PA%40mail.gmail.com</a>.<br>
</blockquote></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/CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJR=
uyuRFT3tos93iA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">htt=
ps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAMDbWJH0JuEzBgDr=
bRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA%40mail.gmail.com</a>.<br />

--000000000000db7bba057be20fc2--

.
