220 41148 <CAB+hmstdjiuT7Z8+_pyRjCNZJ4p7o7gyHD+KhB8yYRuZEiKRUA@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: =?UTF-8?Q?Micha=C5=82_Gawron?= <mcvsama@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 15:10:15 +0100
Lines: 99
Approved: news@gmane.org
Message-ID: <CAB+hmstdjiuT7Z8+_pyRjCNZJ4p7o7gyHD+KhB8yYRuZEiKRUA@mail.gmail.com>
References: <CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq=n2JTkUcHHE6Qbux-PA@mail.gmail.com>
 <CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: blaine.gmane.org 1543586903 29826 195.159.176.226 (30 Nov 2018 14:08:23 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Fri, 30 Nov 2018 14:08:23 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCRMZK6MQMOBBU4JQXQAKGQEG4D7WJI@isocpp.org Fri Nov 30 15:08:19 2018
Return-path: <std-proposals+bncBCRMZK6MQMOBBU4JQXQAKGQEG4D7WJI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yw1-f72.google.com ([209.85.161.72])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCRMZK6MQMOBBU4JQXQAKGQEG4D7WJI@isocpp.org>)
	id 1gSjSs-0007dl-0i
	for gclcip-std-proposals@m.gmane.org; Fri, 30 Nov 2018 15:08:18 +0100
Original-Received: by mail-yw1-f72.google.com with SMTP id t17sf3710844ywc.23
        for <gclcip-std-proposals@m.gmane.org>; Fri, 30 Nov 2018 06:10:28 -0800 (PST)
ARC-Seal: i=2; a=rsa-sha256; t=1543587028; cv=pass;
        d=google.com; s=arc-20160816;
        b=BuhUBmtXWnkoEWE1t+QN0CntBqsDC6ua6XfyMWTkqL288UKtat4BaoEfnHtQEx09DS
         GeQYPC26/fs/1qnZlPhX2aBF9qX4Wte577ap6PApy6/BsBnwlw55Y9HdwOh461j/5FQt
         uoX/qS8hLQB4Rl2MViUJYrSb+MXztKimFP8RM//2mhlMoTiDZB79k7gC+cku47cnkILt
         DYvAnM5QoSHjXs6807ODnx98neQaP+qFk7GU23NAV83RAKnv371ErXLi31qIw/j48ajN
         8AfZ4WCNJxUty+oC5eq+SDBz7Gr9QEEVHuDjcGglmm/qlY+NOXvE4pJaAFDOksJ2HJsb
         ZiZw==
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:content-transfer-encoding
         :to:subject:message-id:date:from:in-reply-to:references:mime-version
         :dkim-signature;
        bh=7DELUh68zEwcPDQ1uwNf4IHdXSozPiYH/HhzZu/7PXI=;
        b=m7Rw455ZhzbY3/8Ida9y3KKtfz4otqtoiv30yIuR0YlhVeGWh6BfPCLRZOgUpLC1y2
         URI9Kr+2NwrPXs0JElVcS4arYScnxbwRRW4ibomSddXzbTOHqubM6s5g5BVOmhWC/KIY
         7WuiSohj+DluJ9/pCGKDQwf+5TYYCjGtdxzBZhVYWL8TSHHBPCqojKn6VRn6tzx/uc+4
         Le/WyDC1J6+JgpeQXN2Irw0OyNpGcUtH4PnTE4654o1DA29GF99eg7TcpCiXfQ93yYqQ
         WrOVjbc61O+O2MibCZlBkE/aY/3XlHw+FGMJVlps465CoN63BTVUp4qProHsI90RLioa
         3bLA==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=GIAE5Fkj;
       spf=pass (google.com: domain of mcvsama@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=mcvsama@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
         :content-transfer-encoding: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=7DELUh68zEwcPDQ1uwNf4IHdXSozPiYH/HhzZu/7PXI=;
        b=z6FW8iRY/jV9QKFzEzMAq8Pa89CMjxzNtzW2y2c8FhqFX0FeWzqldT68dj8XaI7sbs
         MMhgsULburTFrNxi+m0UbABHY1iGuT1VjEx5g1++sk1NkBimYhytJGoc1TPmWOKlxrEX
         hE3WBS+btAxysCWMnCgNk/WJhePORyRYckwklg0AGP+/l6JqBQVglpNYDaS9oaTmJSt7
         /U358KMbY8aKX3v3ZMbtzwg+onsN39y6PkWN5uhC7bObPXwDybMR+RS+A4UajpFjAXWf
         ki6BHiVUiIkuwW+BirGQbBMAsxNjHHIKu8rP7QVbwu8sUBop6BGJ1r3c74B3rAFhAdha
         jvCg==
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:content-transfer-encoding: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=7DELUh68zEwcPDQ1uwNf4IHdXSozPiYH/HhzZu/7PXI=;
        b=ntxiOB0+vQNNFM3hCydwUUZdGApSZTSSY7uJzK+0Cq1q6EpFI9Yq3QPzSyO9AQ9UY9
         VQhEHxLcykkq496nLliGUGV9nV3Qll6TOLWeJTZuPo/9BxLjd2VQgxTGgJBm+5T7fKl3
         x0dHgFIAEgNT5k/imEHLC4A7tOcWkEIvD0l13yIyjcEjHY7P8Bze5TOLl9ClFtrqC5fl
         YoRTqD9uPL7G0Dd89TQkf5WC+zq2ULI6nXBiumnD5FC10s7BZSV2wtI44upMA+F8Zd78
         L/hKkpwAZLqgifD3acNdaTTTQrSUirAbpLRQs7Q2NHj+au9N9mI/5yvlrseb6xI4fs7z
         TaM 
X-Gm-Message-State: AA+aEWYVfLn1X/Wt9Vz6Lu1Owj/x173DdaBBoIyr4XWalRPXzpbVQVuQ
	nnnSb4Jyja+z6l1rHEKZQjeODQ==
X-Google-Smtp-Source: AFSGD/XbPb4Z2F/I6dC5Z7XzNbOaTSKhGVfA+booq93FsN+Qg140i1y3zSdV7IVkkU8gP7EC+AORug==
X-Received: by 2002:a81:2f4f:: with SMTP id v76mr3302252ywv.20.1543587028376;
        Fri, 30 Nov 2018 06:10:28 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:a81:3b0f:: with SMTP id i15ls2965374ywa.7.gmail; Fri, 30 Nov
 2018 06:10:27 -0800 (PST)
X-Received: by 2002:a81:49cc:: with SMTP id w195mr5507210ywa.86.1543587027370;
        Fri, 30 Nov 2018 06:10:27 -0800 (PST)
ARC-Seal: i=1; a=rsa-sha256; t=1543587027; cv=none;
        d=google.com; s=arc-20160816;
        b=xIp2GIJr0lQW4nyPuyScxg2CIpmcgWftyatLSBMdSlqkAX3gOi+LEUzF49qtLo5twP
         2BlBicY5ejX9hbW+nLLSZ+DD4h5EEpIa5MJgfy1TF8TCPQtJcN+lWuYR5RpEZ3gp7w5y
         NOSHumil6igB155VoyRh0WkfxaX3U8LnfRWsdzZ0xN6sJ2Cp1qIpUmuPyM0IB9OtiakJ
         KgAxGNYZaTxjGHXRTv1m4TGjRfLKYmuGQymKnyAIW4Dj6Qv/emr/c3T7FBcsQq8NynCq
         f7Ml7cBCWnZDEYZPo7llOJ3tMQbW8I1nmNfmbfGGlwhAWZpN0YkCz6BGFzftuD+8K8TA
         dttg==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=content-transfer-encoding:to:subject:message-id:date:from
         :in-reply-to:references:mime-version:dkim-signature;
        bh=AffubXDi7KQinlnxmzsjnv5NriXucioehBZ4ieU/8oY=;
        b=mobWjkr+I6aJpx+UJX6lbilD4A4DEMIsWQKFtC1ofcn4900uxNQ+nPoi86O5Z8uFFm
         UlMgG5cEq+KhB4zIRTAGDdmj6Op42h6EdIyCsf9OCVxsHhX6TERP6nTuVhbdKJv8YUyR
         +ssiklBl55H3v2Q5y3tMv199B9bh7lGVojJXoCjxn1U2OyXvfF3y87mUhjmIURJxkHI4
         8zHcTHNIbfDrJyR3XXzuqnqUy8TMdpSN6rm4tqihsOsVg4q8tcEGqCjyxpEKRcFXP2Dj
         2i3DNvqzzJtJsE5gXa8H6ng9+syRz8S4ZFpueKAvCIL50lYhE7A70tR1mf0AskbCQfEg
         uwkw==
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=GIAE5Fkj;
       spf=pass (google.com: domain of mcvsama@gmail.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=mcvsama@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 r1-v6sor887336ywc.170.2018.11.30.06.10.27
        for <std-proposals@isocpp.org>
        (Google Transport Security);
        Fri, 30 Nov 2018 06:10:27 -0800 (PST)
Received-SPF: pass (google.com: domain of mcvsama@gmail.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41;
X-Received: by 2002:a0d:c644:: with SMTP id i65-v6mr5694315ywd.495.1543587026515;
 Fri, 30 Nov 2018 06:10:26 -0800 (PST)
In-Reply-To: <CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3tos93iA@mail.gmail.com>
X-Original-Sender: mcvsama@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com header.s=20161025 header.b=GIAE5Fkj;       spf=pass
 (google.com: domain of mcvsama@gmail.com designates 209.85.220.41 as
 permitted sender) smtp.mailfrom=mcvsama@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:41148
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/41148>

Vu, I wonder what you mean by saying "an instance", is that a separate
object (a copy) or a reference? In this concrete example, change
static_cast to B& instead of B, it will compile and work.
Of course it's far from ideal when you have to static_cast to derived class=
es.
pt., 30 lis 2018 o 14:49 Phil Miller <unmobile@gmail.com> napisa=C5=82(a):
>
> 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 r=
eference 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 a=
voided.
>
> On Fri, Nov 30, 2018, 7:38 AM Vu Pham <phvu225@gmail.com wrote:
>>
>> While thinking about my (ahem) streaming proposal, I think I found a mis=
sing feature in C++. Since this group is highly populated with people speak=
ing 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&, apparen=
tly there is no way to make it work the way I want. Here are a few alternat=
ives I tried:
>>
>> - Make f1() returns A*, then cast the returned pointer. This doesn't loo=
k really nice.
>> - Change the return type of f1() to something like decltype(*this) or au=
to, none of this seems to work.
>>
>> This issue may get worse if A and B are templated, or in a more complica=
ted 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 chi=
ld class of B.
>>
>> Since C++ allows multiple inheritence, and in the same light with the co=
ncept idea that are coming up in new C++, should we coin a new annotation t=
o indicate that a function returns an instance of this class? The actual re=
turned 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 Group=
s "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n 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/iso=
cpp.org/d/msgid/std-proposals/CAFhKpXFOV9DSmmrC3HdGF7Fr44fRzpq%3Dn2JTkUcHHE=
6Qbux-PA%40mail.gmail.com.
>
> --
> 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/isoc=
pp.org/d/msgid/std-proposals/CAMDbWJH0JuEzBgDrbRVd6jxNtQ2FMX377iJRuyuRFT3to=
s93iA%40mail.gmail.com.

--=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/CAB%2BhmstdjiuT7Z8%2B_pyRjCNZJ4p7o7gyHD%2BKhB8yY=
RuZEiKRUA%40mail.gmail.com.

.
