220 26555 <7E3409C2-A2AC-4562-BB06-6D5D5ACE8AC8@gmail.com> article
Path: news.gmane.org!not-for-mail
From: Jonathan Coe <jonathanbcoe@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: auto type deduction for *_cast return argument
Date: Tue, 5 Jul 2016 08:59:03 +0100
Lines: 163
Approved: news@gmane.org
Message-ID: <7E3409C2-A2AC-4562-BB06-6D5D5ACE8AC8@gmail.com>
References: <58140e46-2adc-4eac-8f51-72d3685cdf0b@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0 (1.0)
Content-Type: multipart/alternative;
	boundary=Apple-Mail-F1A4DED3-8A03-4DCE-AA0E-20125957F19A
Content-Transfer-Encoding: 7bit
X-Trace: ger.gmane.org 1467705552 10719 80.91.229.3 (5 Jul 2016 07:59:12 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 5 Jul 2016 07:59:12 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBC2JVFPBRAHBBSOR5W5QKGQEVUA4KSA@isocpp.org Tue Jul 05 09:59:08 2016
Return-path: <std-proposals+bncBC2JVFPBRAHBBSOR5W5QKGQEVUA4KSA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-wm0-f71.google.com ([74.125.82.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBC2JVFPBRAHBBSOR5W5QKGQEVUA4KSA@isocpp.org>)
	id 1bKLG6-0004UA-Ij
	for gclcip-std-proposals@m.gmane.org; Tue, 05 Jul 2016 09:59:06 +0200
Original-Received: by mail-wm0-f71.google.com with SMTP id r190sf81182405wmr.0
        for <gclcip-std-proposals@m.gmane.org>; Tue, 05 Jul 2016 00:59:06 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=from:content-transfer-encoding:mime-version:subject:message-id:date
         :references:in-reply-to: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=6CQG6sURny/najvKvLDN//HJDYPdGXnaSMwaMn6fyjQ=;
        b=1/cCrh4SjnVZQ85opUC8S9E4HLCBb3InEuFzFAk138JDm1UNQh6blVCtqIBUKxfQzl
         eiZcNFErTCSH2TQwe7SYcSejXoTBIQqBHg9hhm5Q8oWYZtA0wgHXHf8VUh/IOapTEbQI
         gcnw6uwhfNX4LmvgaUf5ttr8ZR4HYVhhEjgySmuXB2nonuhu9PoH2oaMbwDOhKmLldnh
         VFkSLHOZVPk7bMWO/juDSV8AwQqdlRcOYImS6zq6iiSGpDBwmH5P+uHaRu3qm1bN1vIs
         +PDrzoxpEDgzry35W3V6y1+7ZelsED2GNJzg24nEymusTrqtuTnBzkX3tNNDdG8HJth1
         p2cg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:content-transfer-encoding:mime-version
         :subject:message-id:date:references:in-reply-to: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=6CQG6sURny/najvKvLDN//HJDYPdGXnaSMwaMn6fyjQ=;
        b=Klx5q9rZfXFpoaZpK/cjkUu+MNQAEsYRiTYvgmWNx69JQu/d9Cn+zOESafo9tr5FUJ
         w70y+to8WJ/51Y5Sho9sQ1SlCgLVnPiXzX5/sI0Z6oEElq8RIKftOnRdXXwIQPXKx1ID
         bltz16mZojKLWvfT9VNEp/LmNjgTiymb0Py0LhJPCo2NR6oZpm29+GDXnxb8w67dD3IZ
         JhdsQLcEI3WdkXfg3HbE+o1o7UQPlxkSEyG4R2F1loJgF5+40/93L3uGA1ICqGBif2PA
         ZAilR2IFk2FDuAUM1JT7/OIPFnNXWaFyQGrHKrho3qu2TvKTu8rnooD6jFpDYxMlNuoh
         R7a 
X-Gm-Message-State: ALyK8tIrPNvpY3Sot6Xm3lL/4/0qNkhC9l9o7fN3YtpXnRSXL4ZXXN79lpO8JKKuW/Gytg==
X-Received: by 10.194.22.169 with SMTP id e9mr6478271wjf.6.1467705545871;
        Tue, 05 Jul 2016 00:59:05 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.134.13 with SMTP id i13ls354696wmd.15.canary; Tue, 05 Jul
 2016 00:59:04 -0700 (PDT)
X-Received: by 10.28.165.5 with SMTP id o5mr14720466wme.87.1467705544780;
        Tue, 05 Jul 2016 00:59:04 -0700 (PDT)
Original-Received: from mail-wm0-x234.google.com (mail-wm0-x234.google.com. [2a00:1450:400c:c09::234])
        by mx.google.com with ESMTPS id k135si52480wmg.61.2016.07.05.00.59.04
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Tue, 05 Jul 2016 00:59:04 -0700 (PDT)
Received-SPF: pass (google.com: domain of jonathanbcoe@gmail.com designates 2a00:1450:400c:c09::234 as permitted sender) client-ip=2a00:1450:400c:c09::234;
Original-Received: by mail-wm0-x234.google.com with SMTP id r201so140883996wme.1
        for <std-proposals@isocpp.org>; Tue, 05 Jul 2016 00:59:04 -0700 (PDT)
X-Received: by 10.28.187.134 with SMTP id l128mr13569674wmf.51.1467705544391;
        Tue, 05 Jul 2016 00:59:04 -0700 (PDT)
Original-Received: from [192.168.1.71] (host86-163-90-41.range86-163.btcentralplus.com. [86.163.90.41])
        by smtp.gmail.com with ESMTPSA id s67sm1914844wmf.3.2016.07.05.00.59.03
        for <std-proposals@isocpp.org>
        (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
        Tue, 05 Jul 2016 00:59:03 -0700 (PDT)
In-Reply-To: <58140e46-2adc-4eac-8f51-72d3685cdf0b@isocpp.org>
X-Mailer: iPhone Mail (13F69)
X-Original-Sender: jonathanbcoe@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 jonathanbcoe@gmail.com designates 2a00:1450:400c:c09::234 as permitted
 sender) smtp.mailfrom=jonathanbcoe@gmail.com;       dmarc=pass (p=NONE
 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:26555
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/26555>


--Apple-Mail-F1A4DED3-8A03-4DCE-AA0E-20125957F19A
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Thanks for suggesting this, I'm afraid I don't think I like this idea.

It saves a bit of typing but would make it less clear what's going on and i=
n cases where a function signature changes would change the meaning of exis=
ting code without any edits being made

f(A a);

Changed to

f(B b);

Would change the meaning of=20

f(dynamic_cast(x));

In a rather non-obvious way.

Regards

Jon

> On 5 Jul 2016, at 08:47, xavinaar@gmail.com wrote:
>=20
> When using reinterpret_cast, dynamic_cast, static_cast and const_cast, yo=
u always have to specify type explicitly, yet it almost always can be deduc=
ed from the context.
>=20
> Consider an example:
>=20
>     void someFunc(Derived *d);
>=20
>     // ...
>=20
>     const Base *cbase =3D new Base;
>     const Derived *cderived =3D dynamic_cast<const Derived *>(cbase);
>     Derived *derived =3D const_cast<Derived *>(cderived);
>     someFunc(derived);
>=20
> Having knowledge of desired argument type (such as explicitly declared va=
riable type or function argument type) we can auto-deduce desired return ty=
pes. My suggestion is to make type specialization for cast operations optio=
nal in such cases which will result in much cleaner code:
>=20
>     const Base *cbase =3D new Base;
>     someFunc(dynamic_cast(const_cast(cbase)));
>=20
> Furthermore, as soon as const_cast is mostly used to cast const pointers/=
references to mutable ones (backwards conversion is implicit), I also sugge=
st to define default behavior of const_cast as to return non-const pointer/=
reference to argument type, such as:
>=20
>     const Base *cbase =3D new Base;
>     auto base =3D const_cast(cbase); // type of base is "Base"
>=20
> This rule can also be generalized to any templated function:
>=20
>     template <typename T>
>     T someFunc();
>=20
>     // ...
>=20
>     int i =3D someFunc(); // will invoke someFunc<int>();
> --=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=
 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/58140e46-2adc-4eac-8f51-72d3685cdf0b%40isocpp.=
org.

--=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/7E3409C2-A2AC-4562-BB06-6D5D5ACE8AC8%40gmail.com=
..

--Apple-Mail-F1A4DED3-8A03-4DCE-AA0E-20125957F19A
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=
=3Dutf-8"></head><body dir=3D"auto"><div></div><div>Thanks for suggesting t=
his, I'm afraid I don't think I like this idea.</div><div><br></div><div>It=
 saves a bit of typing but would make it less clear what's going on and in =
cases where a function signature changes would change the meaning of existi=
ng code without any edits being made</div><div><br></div><div>f(A a);</div>=
<div><br></div><div>Changed to</div><div><br></div><div>f(B b);</div><div><=
br></div><div>Would change the meaning of&nbsp;</div><div><br></div><div>f(=
dynamic_cast(x));</div><div><br></div><div>In a rather non-obvious way.</di=
v><div><br></div><div>Regards</div><div><br></div><div>Jon</div><div><br>On=
 5 Jul 2016, at 08:47, <a href=3D"mailto:xavinaar@gmail.com">xavinaar@gmail=
..com</a> wrote:<br><br></div><blockquote type=3D"cite"><div><div dir=3D"ltr=
">When using<b> reinterpret_cast,</b> <b>dynamic_cast</b>, <b>static_cast</=
b> and <b>const_cast</b>, you always have to specify type explicitly, yet i=
t almost always can be deduced from the context.<br><br>Consider an example=
:<br><br>&nbsp;&nbsp;&nbsp; void someFunc(Derived *d);<br><br>&nbsp;&nbsp;&=
nbsp; // ...<br><br>&nbsp;&nbsp;&nbsp; const Base *cbase =3D new Base;<br>&=
nbsp;&nbsp;&nbsp; const Derived *cderived =3D dynamic_cast&lt;const Derived=
 *&gt;(cbase);<br>&nbsp;&nbsp;&nbsp; Derived *derived =3D const_cast&lt;Der=
ived *&gt;(cderived);<br>&nbsp;&nbsp;&nbsp; someFunc(derived);<br><br>Havin=
g knowledge of desired argument type (such as explicitly declared variable =
type or function argument type) we can auto-deduce desired return types. My=
 suggestion is to make type specialization for cast operations optional in =
such cases which will result in much cleaner code:<br><br>&nbsp;&nbsp;&nbsp=
; const Base *cbase =3D new Base;<br>&nbsp;&nbsp;&nbsp; someFunc(dynamic_ca=
st(const_cast(cbase)));<br><br>Furthermore, as soon as <b>const_cast</b> is=
 mostly used to cast const pointers/references to mutable ones (backwards c=
onversion is implicit), I also suggest to define default behavior of <b>con=
st_cast</b> as to return non-const pointer/reference to argument type, such=
 as:<br><br>&nbsp;&nbsp;&nbsp; const Base *cbase =3D new Base;<br>&nbsp;&nb=
sp;&nbsp; auto base =3D const_cast(cbase); // type of base is "Base"<br><br=
>This rule can also be generalized to any templated function:<br><br>&nbsp;=
&nbsp;&nbsp; template &lt;typename T&gt;<br>&nbsp;&nbsp;&nbsp; T someFunc()=
;<br><br>&nbsp;&nbsp;&nbsp; // ...<br><br>&nbsp;&nbsp;&nbsp; int i =3D some=
Func(); // will invoke someFunc&lt;int&gt;();<br></div>

<p></p>

-- <br>
You received this message because you are subscribed to the Google Groups "=
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/58140e46-2adc-4eac-8f51-72d3685cdf0b%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter">https://groups.goo=
gle.com/a/isocpp.org/d/msgid/std-proposals/58140e46-2adc-4eac-8f51-72d3685c=
df0b%40isocpp.org</a>.<br>
</div></blockquote></body></html>

<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/7E3409C2-A2AC-4562-BB06-6D5D5ACE8AC8%=
40gmail.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/7E3409C2-A2AC-4562-BB06-6D5D5ACE8AC8%=
40gmail.com</a>.<br />

--Apple-Mail-F1A4DED3-8A03-4DCE-AA0E-20125957F19A--

.
