220 20630 <09547076-d049-4d87-acc3-b851e6518942@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: joewoodbury@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Null -conditional and -coalescing operators
Date: Tue, 22 Sep 2015 10:38:58 -0700 (PDT)
Lines: 163
Approved: news@gmane.org
Message-ID: <09547076-d049-4d87-acc3-b851e6518942@isocpp.org>
References: <4c3e2ee7-2159-4e93-a439-7d5c94c38c9c@isocpp.org>
 <6a0ad653-4764-483b-afb5-74d444be15d9@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_284_642464533.1442943538915"
X-Trace: ger.gmane.org 1442943545 2981 80.91.229.3 (22 Sep 2015 17:39:05 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Tue, 22 Sep 2015 17:39:05 +0000 (UTC)
Cc: joewoodbury@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCBMHLUN7EEBBM5EQ2YAKGQEJHMQFEI@isocpp.org Tue Sep 22 19:39:04 2015
Return-path: <std-proposals+bncBCBMHLUN7EEBBM5EQ2YAKGQEJHMQFEI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pd0-f197.google.com ([209.85.192.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCBMHLUN7EEBBM5EQ2YAKGQEJHMQFEI@isocpp.org>)
	id 1ZeRWw-0000Mj-HG
	for gclcip-std-proposals@m.gmane.org; Tue, 22 Sep 2015 19:39:02 +0200
Original-Received: by pdbpr5 with SMTP id pr5sf25979423pdb.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 22 Sep 2015 10:39:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:cc:message-id:in-reply-to:references:subject
         :mime-version:content-type: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=GCzxwn0DJujgnVf+ivuhTFnXKqmQbkmQ4G7Q++kbT6I=;
        b=tLardDEAYxa9jviRl8bXfL4qASrjAtVN6XNBpgpEhw8e8IObphoRjbMnK9pzt6dGTx
         PDdsLLRf/JPaSJtmIWjJoneL9f7+qpeo3dA1SPGJzwu/i+Xltu6vIQGIwwCerlxDGIOt
         zV6D0RFq7cqppmkZdJLmf/3I9/kw3n0h6GwOEQoV8NQ9OkeCZvkgREfLfGBcqjJWk8rc
         8xVuYlw5QQyDiE2FUZQA59doCh6geWKk1jllwlxMf9DdPawQnYu364HxYFbDTTy/JanI
         v8/u0CqVhBaYT0/xgxcaQABqggTEZ/QaW7x4IeUGFQFzF5Q2yIQtEOPMrD5OD+tuJ61k
         c9lQ==
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:cc:message-id:in-reply-to
         :references:subject:mime-version:content-type: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=GCzxwn0DJujgnVf+ivuhTFnXKqmQbkmQ4G7Q++kbT6I=;
        b=Wr4qvD/IxBt2/XErM7cRm3gUVd2722A1/O3SRI/nydw3+eo2W3ITp4IHcswFdQSwru
         6n+WVfpHpT7fKQxmEUa1IiivmLM+OV+EFvgarPSphTPUHOrl+oVqj8ZEEAtffVMfMjHn
         pSbqf8Sz8etWp8GW1yxGY1sM9VVqvF8wcnzp5Ezv6wxTiJ1Qesrz30CReYVGD0mvF6sj
         8ljsGSzls3lNeLdoINf/kG32VqGN1w7rlKLbQfZkQ9b4yigfyLeICH3Qcm0zrWj+wyMd
         giWr9bAKpPdnpaResFuYrrRUVHV3b9Sel7cWpQaQppHt7aKkHd7eVESM27TOEZSuEv7+
         aYxA==
X-Gm-Message-State: ALoCoQmzFwtmTJOa5iIY+3HFKjwvDVJ4n3fObvoER94sa2mS5/Ci7B3t4e0DcWjse12ettbvMRQ1
X-Received: by 10.66.182.162 with SMTP id ef2mr23055644pac.35.1442943541423;
        Tue, 22 Sep 2015 10:39:01 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.132.84 with SMTP id g81ls199067iod.98.gmail; Tue, 22 Sep
 2015 10:38:59 -0700 (PDT)
X-Received: by 10.50.45.100 with SMTP id l4mr210980igm.9.1442943539650;
        Tue, 22 Sep 2015 10:38:59 -0700 (PDT)
In-Reply-To: <6a0ad653-4764-483b-afb5-74d444be15d9@isocpp.org>
X-Original-Sender: joewoodbury@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: <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:20630
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/20630>

------=_Part_284_642464533.1442943538915
Content-Type: multipart/alternative; 
	boundary="----=_Part_285_1814952225.1442943538915"

------=_Part_285_1814952225.1442943538915
Content-Type: text/plain; charset=UTF-8

First, I didn't say it was necessary nor even imply it; I said it would be 
"a nice solution"

Second, my examples were intentionally trivial.

Thus if I wrote

int val = connection ?: 5;

your solution wouldn't work.


On Tuesday, September 22, 2015 at 10:15:58 AM UTC-7, Nicol Bolas wrote:
>
> On Tuesday, September 22, 2015 at 1:03:47 PM UTC-4, joewo...@gmail.com 
> wrote:
>>
>> Checking for null and then doing one thing is a common task. 
>> null-conditional and null-coalescing operators would be a nice solution.
>>
>> For example, given the following pointer:
>>
>>     unique_ptr<Connection> connection;
>>
>> This may happen:
>>
>>     if (connection)
>>     {
>>         connection->sendMessage(msg);
>>     }
>>
>> A nice alternative would be:
>>
>>     connection ?: connection->SendMessage(msg);
>>
>
> Or you could have just written:
>
> if(connection) connection->SendMessage(msg);
>
> That requires only one or two extra keystrokes. Both of these are 
> restricted to single lines to be executed.
>
> I've had situations where if anything resolves to false on the left, this 
>> ?: would be useful.
>>
>> Or even better, to carry the concept forward:
>>
>>     connection?->SendMessage(msg);
>>
>
> Ultimately, I don't think good coding practices make this as necessary as 
> you let on. Most of the time, if you have a pointer, you know it's valid. 
> It was checked for validity when you received it, and you don't want to 
> constantly check to see if the thing that was valid before remains valid 
> now.
>
> We don't need to use up precious syntax for a relatively infrequent case.
>
> Likewise:
>>
>>     bool success = connection ?: false;
>>
>
> Also easily done like this:
>
> auto success = bool{connection};
>
> `explicit operator bool` will be called here, since you explicitly asked 
> for it.
>

-- 

--- 
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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_285_1814952225.1442943538915
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">First, I didn&#39;t say it was necessary nor even imply it=
; I said it would be &quot;a nice solution&quot;<br><br>Second, my examples=
 were intentionally trivial.<br><br>Thus if I wrote<br><br>int val =3D conn=
ection ?: 5;<br><br>your solution wouldn&#39;t work.<br><br><br>On Tuesday,=
 September 22, 2015 at 10:15:58 AM UTC-7, Nicol Bolas wrote:<blockquote cla=
ss=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #=
ccc solid;padding-left: 1ex;"><div dir=3D"ltr">On Tuesday, September 22, 20=
15 at 1:03:47 PM UTC-4, <a>joewo...@gmail.com</a> wrote:<blockquote class=
=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc s=
olid;padding-left:1ex"><div dir=3D"ltr">Checking for null and then doing on=
e thing is a common task. null-conditional and null-coalescing operators wo=
uld be a nice solution.<br><br>For example, given the following pointer:<br=
><br>=C2=A0=C2=A0=C2=A0 unique_ptr&lt;Connection&gt; connection;<br><br>Thi=
s may happen:<br><br>=C2=A0=C2=A0=C2=A0 if (connection)<br>=C2=A0=C2=A0=C2=
=A0 {<br>=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0 connection-&gt;sendMessage(m=
sg);<br>=C2=A0=C2=A0=C2=A0 }<br><br>A nice alternative would be:<br><br>=C2=
=A0=C2=A0=C2=A0 connection ?: connection-&gt;SendMessage(msg);<br></div></b=
lockquote><div><br>Or you could have just written:<br><br><div style=3D"bac=
kground-color:rgb(250,250,250);border-color:rgb(187,187,187);border-style:s=
olid;border-width:1px;word-wrap:break-word"><code><div><span style=3D"color=
:#008">if</span><span style=3D"color:#660">(</span><span style=3D"color:#00=
0">connection</span><span style=3D"color:#660">)</span><span style=3D"color=
:#000"> connection</span><span style=3D"color:#660">-&gt;</span><span style=
=3D"color:#606">SendMessage</span><span style=3D"color:#660">(</span><span =
style=3D"color:#000">msg</span><span style=3D"color:#660">);</span></div></=
code></div><br>That requires only one or two extra keystrokes. Both of thes=
e are restricted to single lines to be executed.<br><br></div><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #c=
cc solid;padding-left:1ex"><div dir=3D"ltr">I&#39;ve had situations where i=
f anything resolves to false on the left, this ?: would be useful.<br><br>O=
r even better, to carry the concept forward:<br><br>=C2=A0=C2=A0=C2=A0 conn=
ection?-&gt;SendMessage(msg);<br></div></blockquote><div><br>Ultimately, I =
don&#39;t think good coding practices make this as necessary=20
as you let on. Most of the time, if you have a pointer, you know it&#39;s=
=20
valid. It was checked for validity when you received it, and you don&#39;t=
=20
want to constantly check to see if the thing that was valid before=20
remains valid now.<br><br>We don&#39;t need to use up precious syntax for a=
 relatively infrequent case.<br><br></div><blockquote class=3D"gmail_quote"=
 style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><div dir=3D"ltr">Likewise:<br><br>=C2=A0=C2=A0=C2=A0 bool success =
=3D connection ?: false;<br></div></blockquote><div><br>Also easily done li=
ke this:<br><br><div style=3D"background-color:rgb(250,250,250);border-colo=
r:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-word=
"><code><div><span style=3D"color:#008">auto</span><span style=3D"color:#00=
0"> success </span><span style=3D"color:#660">=3D</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#008">bool</span><span style=3D"color:=
#660">{</span><span style=3D"color:#000">connection</span><span style=3D"co=
lor:#660">};</span></div></code></div><br>`explicit operator bool` will be =
called here, since you explicitly asked for it.<br></div></div></blockquote=
></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_285_1814952225.1442943538915--
------=_Part_284_642464533.1442943538915--

.
