220 26554 <58140e46-2adc-4eac-8f51-72d3685cdf0b@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: xavinaar@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: auto type deduction for *_cast return argument
Date: Tue, 5 Jul 2016 00:47:02 -0700 (PDT)
Lines: 102
Approved: news@gmane.org
Message-ID: <58140e46-2adc-4eac-8f51-72d3685cdf0b@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1397_473365688.1467704822121"
X-Trace: ger.gmane.org 1467704826 31920 80.91.229.3 (5 Jul 2016 07:47:06 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 5 Jul 2016 07:47:06 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCX4LEEDVMDRB56L5W5QKGQERQFCXTY@isocpp.org Tue Jul 05 09:47:06 2016
Return-path: <std-proposals+bncBCX4LEEDVMDRB56L5W5QKGQERQFCXTY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yw0-f200.google.com ([209.85.161.200])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCX4LEEDVMDRB56L5W5QKGQERQFCXTY@isocpp.org>)
	id 1bKL4T-0006LC-4p
	for gclcip-std-proposals@m.gmane.org; Tue, 05 Jul 2016 09:47:05 +0200
Original-Received: by mail-yw0-f200.google.com with SMTP id i12sf137079756ywa.0
        for <gclcip-std-proposals@m.gmane.org>; Tue, 05 Jul 2016 00:47:04 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:x-spam-checked-in-group
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=vJgK22uHxZi582hEIBB2mczqgz9A6wklH1Go8pECXPY=;
        b=aVpD/kk0qC0Iq72ThhL1giR53a+MfcLV53FU1k38v+rIZHlVBUoaJGMeiucsGBqIhq
         IOYARoJ0T6WHLhoFa7DARBA8oahTOdEMjN0GJt//RGATshKf43rTjW5cuxs2drGHQM3I
         gJo4W0KX/A+fmauIh7feCgo07O0N7p4KdzoGC22KL9UBN5buGBpWjjt7XafxhUt9X1mG
         ve7MAAykuzoQjm/c6LIlsRA6BHwwUZDF/bu+p0BFzjohikn+bdxvVb5hTpy2JwcTTkan
         Qk7llS5/YnsS9hDrwx3pBzaJIso0dSFBfbJK8oKJuNWdUBWK8KWZ6W0XVIfqc0j8SIgx
         DAdQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:x-spam-checked-in-group
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=vJgK22uHxZi582hEIBB2mczqgz9A6wklH1Go8pECXPY=;
        b=uZVECnjcWnM076feTeONmllA6Ut9LgwmcJC6lCqNecd/qtBD5DrDmMicHfV5whWBNU
         TOajKO/RtH197+bLLnray0WDoxGkCkKBsu3icAz1+JVKL73QvYO5gXaRjF6jrkhiU0tY
         jY5Nk4zAksRYXW7EKmfBCTVy7SJrTXrplXEnBxEuUgRYf+OMKjkXy95XPlAiMGCX/toD
         EeQPspL6v1kp58YVpM136C2Pc4CP1lqzhby9gpkMguRC888AHx/9cToV5PP6+hDEv0Sr
         ZrJySEBOV/kzVLhd530Tu6/H1GrpuJPLL5R8fZNiipzHnkhAc8KNiQT1hM8DiLYCIOvW
         9sgw==
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:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=vJgK22uHxZi582hEIBB2mczqgz9A6wklH1Go8pECXPY=;
        b=JPx8i0nsVYmliGBcPCZYf7U2K9duKwBddpJ+MaLg5X9n0sGbD0i3nIIdo6D3O0ilH4
         tSgrpHdm5YZ69wVGW2FYcORBXMJxAd15OybdimC+VGOJgY3WZeXQ7q+h0kV3R7nXGk5f
         IHDnXn0WTGxR5m2CDUQe4ohhIMP066fDUTM75EIMDc9K3+8mLONLKqh//LQotVPWif5N
         7puzxRibAxkvLiCZGUObmYZKRcVIUSA1Ti5WIPpi2pmimKMztVgaL7ilU5VABYYa14f1
         qbJUEYbxNv9Ven5f/W0O2xAO4jKOC5PATA83CvBzWiRMj0y+uhwN60LDJeTTL/pYLrKk
         JGkg==
X-Gm-Message-State: ALyK8tI10wyXtt/9EJwOiP+vBfA8VqXpyHbjaEKRH+805LVsDg74V/yXgRDDsYHA14Cs4g==
X-Received: by 10.129.75.139 with SMTP id y133mr34717635ywa.44.1467704824206;
        Tue, 05 Jul 2016 00:47:04 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.202.1 with SMTP id a1ls1938198iog.49.gmail; Tue, 05 Jul
 2016 00:47:03 -0700 (PDT)
X-Received: by 10.36.17.15 with SMTP id 15mr337919itf.10.1467704823152;
        Tue, 05 Jul 2016 00:47:03 -0700 (PDT)
X-Original-Sender: Xavinaar@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:26554
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/26554>

------=_Part_1397_473365688.1467704822121
Content-Type: multipart/alternative; 
	boundary="----=_Part_1398_1831151153.1467704822121"

------=_Part_1398_1831151153.1467704822121
Content-Type: text/plain; charset=UTF-8

When using* reinterpret_cast,* *dynamic_cast*, *static_cast* and 
*const_cast*, you always have to specify type explicitly, yet it almost 
always can be deduced from the context.

Consider an example:

    void someFunc(Derived *d);

    // ...

    const Base *cbase = new Base;
    const Derived *cderived = dynamic_cast<const Derived *>(cbase);
    Derived *derived = const_cast<Derived *>(cderived);
    someFunc(derived);

Having 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:

    const Base *cbase = new Base;
    someFunc(dynamic_cast(const_cast(cbase)));

Furthermore, as soon as *const_cast* is mostly used to cast const 
pointers/references to mutable ones (backwards conversion is implicit), I 
also suggest to define default behavior of *const_cast* as to return 
non-const pointer/reference to argument type, such as:

    const Base *cbase = new Base;
    auto base = const_cast(cbase); // type of base is "Base"

This rule can also be generalized to any templated function:

    template <typename T>
    T someFunc();

    // ...

    int i = someFunc(); // will invoke someFunc<int>();

-- 
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/58140e46-2adc-4eac-8f51-72d3685cdf0b%40isocpp.org.

------=_Part_1398_1831151153.1467704822121
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<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 ex=
plicitly, yet it almost always can be deduced from the context.<br><br>Cons=
ider an example:<br><br>=C2=A0=C2=A0=C2=A0 void someFunc(Derived *d);<br><b=
r>=C2=A0=C2=A0=C2=A0 // ...<br><br>=C2=A0=C2=A0=C2=A0 const Base *cbase =3D=
 new Base;<br>=C2=A0=C2=A0=C2=A0 const Derived *cderived =3D dynamic_cast&l=
t;const Derived *&gt;(cbase);<br>=C2=A0=C2=A0=C2=A0 Derived *derived =3D co=
nst_cast&lt;Derived *&gt;(cderived);<br>=C2=A0=C2=A0=C2=A0 someFunc(derived=
);<br><br>Having knowledge of desired argument type (such as explicitly dec=
lared variable type or function argument type) we can auto-deduce desired r=
eturn types. My suggestion is to make type specialization for cast operatio=
ns optional in such cases which will result in much cleaner code:<br><br>=
=C2=A0=C2=A0=C2=A0 const Base *cbase =3D new Base;<br>=C2=A0=C2=A0=C2=A0 so=
meFunc(dynamic_cast(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 conversion is implicit), I also suggest to define default b=
ehavior of <b>const_cast</b> as to return non-const pointer/reference to ar=
gument type, such as:<br><br>=C2=A0=C2=A0=C2=A0 const Base *cbase =3D new B=
ase;<br>=C2=A0=C2=A0=C2=A0 auto base =3D const_cast(cbase); // type of base=
 is &quot;Base&quot;<br><br>This rule can also be generalized to any templa=
ted function:<br><br>=C2=A0=C2=A0=C2=A0 template &lt;typename T&gt;<br>=C2=
=A0=C2=A0=C2=A0 T someFunc();<br><br>=C2=A0=C2=A0=C2=A0 // ...<br><br>=C2=
=A0=C2=A0=C2=A0 int i =3D someFunc(); // will invoke someFunc&lt;int&gt;();=
<br></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/58140e46-2adc-4eac-8f51-72d3685cdf0b%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/58140e46-2adc-4eac-8f51-72d3685cdf0b=
%40isocpp.org</a>.<br />

------=_Part_1398_1831151153.1467704822121--

------=_Part_1397_473365688.1467704822121--

.
