220 37307 <c7cd5d95-0031-4e50-b7a1-9adcff695bd2@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: inkwizytoryankes@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Pointer overload for ||
Date: Sun, 11 Mar 2018 17:28:34 -0700 (PDT)
Lines: 300
Approved: news@gmane.org
Message-ID: <c7cd5d95-0031-4e50-b7a1-9adcff695bd2@isocpp.org>
References: <1fea07df-46a6-4df2-865a-849be8f8d129@isocpp.org>
 <CANiq72kL7HQf2oS5THm8bxzurhzoc7uhNQq3S9+zvBce2yMZZQ@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_12969_1243461468.1520814514135"
X-Trace: blaine.gmane.org 1520814395 3195 195.159.176.226 (12 Mar 2018 00:26:35 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 12 Mar 2018 00:26:35 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDDLTAGNTIBBBM4TS7KQKGQEESW2P2A@isocpp.org Mon Mar 12 01:26:31 2018
Return-path: <std-proposals+bncBDDLTAGNTIBBBM4TS7KQKGQEESW2P2A@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vk0-f69.google.com ([209.85.213.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDDLTAGNTIBBBM4TS7KQKGQEESW2P2A@isocpp.org>)
	id 1evBIL-0000gU-O1
	for gclcip-std-proposals@m.gmane.org; Mon, 12 Mar 2018 01:26:30 +0100
Original-Received: by mail-vk0-f69.google.com with SMTP id l71sf5369742vki.6
        for <gclcip-std-proposals@m.gmane.org>; Sun, 11 Mar 2018 17:28:36 -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:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=yndiy487pfi9A0DrmEsQb6TAzaSI7y3ROMbHYIg/jpg=;
        b=d6/ooZPSRp3qm0VVgZim9bLt6nrwPbyiZ0qPI0Rx5kL9duH+Zc73oiCXyTZh47c8ET
         pJW3DRr9G59teUPNGHgxSShwvYNJXiGZ+CQxixoMSBFDYGqPVh5FQVMulI9jsATmt4tM
         2dnEf60UqFLXZ4zDjSe3nAwzUMmxv6nS3GV5XD0SGLAQNF9Rbvsfrh62X2x49Q1ZpBFP
         uzslkiO3Z6NiBjQVKDFAKA1/pP1TlI3Q+kx7S4neDqvNBEWpwEYXBJD5ACV8HhWh6oMf
         XzwBuIEdBprhYIgU6sLEPmsnwEuG38AOdzuxH6RSIAw9Wf0xfkz1TLNcXecljTdIW5A1
         PFEw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=yndiy487pfi9A0DrmEsQb6TAzaSI7y3ROMbHYIg/jpg=;
        b=qDXMmx2q/xJ7zz7MHMvWjIXrkjK4gNj4JYV3RxJ6Y8W3mLi7SD791qgq6JIznefS5p
         PfQXYnheR0F7o02cNnxliCvy2Vk1n5yMTOEvcr1OV49KE6Lx2BsYcnKVDdWQUitGPM5D
         iwnNAKWqEES4ju69ZrvgjuXfrp1ab0Wlc2jFGbt8/0mqRSg6nm49nk/LqIHt9G9Rgopp
         vgEjgvGFD0A+/GUav/qJ8v3K6UVg3c1LNpiaMr3DOeP0w9dHPAn//lxkX9FAJve6f7wa
         kdfhAWQa4D8rtF8cpkqkR3/hHFCYbVWchXi7+fUubyv9tfHWceBhUYetftiZnfG5cDy7
         fVYw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :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=yndiy487pfi9A0DrmEsQb6TAzaSI7y3ROMbHYIg/jpg=;
        b=b7sxJDwP+Ny0Gg6Jx42gB7TQcQ/x0/SdIB89VhGPFofFDBvu1/TW/yygxrKVWqqGBB
         0a9N77DuGcJFmzHmYCwchoYnKXG0JrWZOu8Nru1LaX+cPkBSbXzDNVICXT70Ni9nksA1
         3cDXIlSCacvs8udyNGjqxi4yzEEjfqrhEfIcRj0dzVC4aexAwtrt+kz+D+38pxLOAXqg
         /z6EAFcNI0ZXQU1LYd4FmeH2KWf/Fuq0h5y/arDfbC/i2OGIbzxJ9o07M8xtroZ1gJiq
         3vNpmZ7/2sXDAzOuVma0mtdqbgg8mso16odyh1xyF1MEJUi3BZxGPFSNqBharvGMQtXS
         EpLQ==
X-Gm-Message-State: AElRT7EZBpNdTS6YS9huzAAttzvmhnPDIWP3ZRH5rhjLd0GlTFOSeaE5
	gZBsxNzx27rtRMRmtgkLX5AiKw==
X-Google-Smtp-Source: AG47ELv5/797wmj2Kl10c6gvO2+JkmSQywiRz2tl/0DPNV89HoACerMPyCLEyj7eIRrtL3TLS8edCw==
X-Received: by 10.31.148.73 with SMTP id w70mr2817679vkd.117.1520814516227;
        Sun, 11 Mar 2018 17:28:36 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.31.185.202 with SMTP id j193ls6622044vkf.18.gmail; Sun, 11 Mar
 2018 17:28:34 -0700 (PDT)
X-Received: by 10.31.178.81 with SMTP id b78mr811522vkf.14.1520814514738;
        Sun, 11 Mar 2018 17:28:34 -0700 (PDT)
In-Reply-To: <CANiq72kL7HQf2oS5THm8bxzurhzoc7uhNQq3S9+zvBce2yMZZQ@mail.gmail.com>
X-Original-Sender: inkwizytoryankes@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:37307
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/37307>

------=_Part_12969_1243461468.1520814514135
Content-Type: multipart/alternative; 
	boundary="----=_Part_12970_1220829327.1520814514135"

------=_Part_12970_1220829327.1520814514135
Content-Type: text/plain; charset="UTF-8"



On Sunday, March 11, 2018 at 7:06:59 PM UTC+1, Miguel Ojeda wrote:
>
> On Sun, Mar 11, 2018 at 9:38 AM,  <jefferson...@gmail.com <javascript:>> 
> wrote: 
> > Not sure how this will be received -- I haven't posted here much.  Just 
> > thought it would be worth mentioning this idea. 
> > 
> > Suppose you have an API that you want to optionally allocate storage for 
> > your user.  They can either pass in a pointer to some existing allocated 
> > space, or pass in a null pointer and space is allocated for them. 
> > 
> > void foo(int *x) { 
> >   if (x == nullptr) { 
> >     x = new int; 
> >   } 
> >   // etc. 
> > } 
> > 
> > Just figured it would be a decent idea if the || operator received a 
> pointer 
> > overload.  Then you could write the more succinct 
> > 
> > void foo(int *x) { 
> >   x = x || new int; 
> > } 
> > 
> > or even (ducks for cover) 
> > 
> > void foo(int *x) { 
> >   x ||= new int; 
> > } 
>
> Your first version is the best one, to be honest :-) In any case, if 
> you really need something like you propose (a pseudo-DSL, generated 
> code, ...?), then a macro with a descriptive name would be the 
> simplest and most readable, e.g. for your second version: 
>
>     #define OR_ELSE(v, e) ((v) ? (v) : (e)) 
>
>     // ... 
>     x = OR_ELSE(x, new int); 
>
> For your third version: 
>
>     #define ENSURE_INIT(v, e) do { if (not (v)) (v) = (e); } while(false) 
>
>     // ... 
>     ENSURE_INIT(x, new int); 
>
>
This solution is not perfect because it reuse `v` multiple times, if value 
is not simply value it could be problematic.

Better would be using lambda similar like in your next point:


#define OR_ELSE(v, e) ([&](auto MACRO_OR_ELSE_VALUE_X){ return (MACRO_OR_ELSE_VALUE_X 
? MACRO_OR_ELSE_VALUE_X : (e)); }(v))



This will allow evaluate `v` only once and can be inserted in other 
expressions:

auto z = h() ? OR_ELSE(OR_ELSE(x, foo()), bar()) : zz();


 

> If you are really keen on having the operator, you could always 
> approximate like: 
>
>     #define LAZY(x) [](){ return (x); } 
>
>     template <typename T, typename E> 
>     T operator||(T p, E e) 
>     { 
>        if (p) 
>            return p; 
>        return e(); 
>     } 
>
>     // ... 
>     x = x || LAZY(new int); 
>
> Similar for |=; and add something to restrict the overloads as needed. 
>
> Now, if you want to make a cool proposal: 
>
>     template <typename T, expression E> 
>     T operator||(T p, E e) 
>     { 
>        if (p) 
>            return p; 
>        return e; 
>     } 
>
>     // ... 
>     x = x || new int; 
>
> ;-) 
>
> Cheers, 
> Miguel 
>

-- 
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/c7cd5d95-0031-4e50-b7a1-9adcff695bd2%40isocpp.org.

------=_Part_12970_1220829327.1520814514135
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, March 11, 2018 at 7:06:59 PM UTC+1, Mig=
uel Ojeda wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin=
-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Sun, Mar 11=
, 2018 at 9:38 AM, =C2=A0&lt;<a href=3D"javascript:" target=3D"_blank" gdf-=
obfuscated-mailto=3D"HV-y43WUAAAJ" rel=3D"nofollow" onmousedown=3D"this.hre=
f=3D&#39;javascript:&#39;;return true;" onclick=3D"this.href=3D&#39;javascr=
ipt:&#39;;return true;">jefferson...@gmail.<wbr>com</a>&gt; wrote:
<br>&gt; Not sure how this will be received -- I haven&#39;t posted here mu=
ch. =C2=A0Just
<br>&gt; thought it would be worth mentioning this idea.
<br>&gt;
<br>&gt; Suppose you have an API that you want to optionally allocate stora=
ge for
<br>&gt; your user. =C2=A0They can either pass in a pointer to some existin=
g allocated
<br>&gt; space, or pass in a null pointer and space is allocated for them.
<br>&gt;
<br>&gt; void foo(int *x) {
<br>&gt; =C2=A0 if (x =3D=3D nullptr) {
<br>&gt; =C2=A0 =C2=A0 x =3D new int;
<br>&gt; =C2=A0 }
<br>&gt; =C2=A0 // etc.
<br>&gt; }
<br>&gt;
<br>&gt; Just figured it would be a decent idea if the || operator received=
 a pointer
<br>&gt; overload. =C2=A0Then you could write the more succinct
<br>&gt;
<br>&gt; void foo(int *x) {
<br>&gt; =C2=A0 x =3D x || new int;
<br>&gt; }
<br>&gt;
<br>&gt; or even (ducks for cover)
<br>&gt;
<br>&gt; void foo(int *x) {
<br>&gt; =C2=A0 x ||=3D new int;
<br>&gt; }
<br>
<br>Your first version is the best one, to be honest :-) In any case, if
<br>you really need something like you propose (a pseudo-DSL, generated
<br>code, ...?), then a macro with a descriptive name would be the
<br>simplest and most readable, e.g. for your second version:
<br>
<br>=C2=A0 =C2=A0 #define OR_ELSE(v, e) ((v) ? (v) : (e))
<br>
<br>=C2=A0 =C2=A0 // ...
<br>=C2=A0 =C2=A0 x =3D OR_ELSE(x, new int);
<br>
<br>For your third version:
<br>
<br>=C2=A0 =C2=A0 #define ENSURE_INIT(v, e) do { if (not (v)) (v) =3D (e); =
} while(false)
<br>
<br>=C2=A0 =C2=A0 // ...
<br>=C2=A0 =C2=A0 ENSURE_INIT(x, new int);
<br>
<br></blockquote><div><br>This solution is not perfect because it reuse `v`=
 multiple times, if value is not simply value it could be problematic.<br><=
br>Better would be using lambda similar like in your next point:<br><br><di=
v style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187=
, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word;"=
 class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subprettyp=
rint"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span>=
<span style=3D"color: #800;" class=3D"styled-by-prettify">#define</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> OR_ELSE</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">v</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> e</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
([&amp;](</span><span style=3D"color: #008;" class=3D"styled-by-prettify">a=
uto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> MACRO_=
OR_ELSE_VALUE_X</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">){</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify">MACRO_OR_ELSE_VALUE_X </span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">?</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify"> MACRO_OR_ELSE_VALUE_X </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify">e</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">));</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">v</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">))</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span></div=
></code></div><br><br>This will allow evaluate `v` only once and can be ins=
erted in other expressions:<br><br><div style=3D"background-color: rgb(250,=
 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-w=
idth: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><code class=3D=
"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">auto</span><span style=3D"color: #000;" class=3D=
"styled-by-prettify"> z </span><span style=3D"color: #660;" class=3D"styled=
-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> h</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(=
)</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">?</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> OR_ELSE</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">OR_ELSE</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify">x</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">()),</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> bar=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">())</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> zz</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">();</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span></div></code></div><br><br>=C2=A0</div=
><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bo=
rder-left: 1px #ccc solid;padding-left: 1ex;">If you are really keen on hav=
ing the operator, you could always
<br>approximate like:
<br>
<br>=C2=A0 =C2=A0 #define LAZY(x) [](){ return (x); }
<br>
<br>=C2=A0 =C2=A0 template &lt;typename T, typename E&gt;
<br>=C2=A0 =C2=A0 T operator||(T p, E e)
<br>=C2=A0 =C2=A0 {
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (p)
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return p;
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return e();
<br>=C2=A0 =C2=A0 }
<br>
<br>=C2=A0 =C2=A0 // ...
<br>=C2=A0 =C2=A0 x =3D x || LAZY(new int);
<br>
<br>Similar for |=3D; and add something to restrict the overloads as needed=
..
<br>
<br>Now, if you want to make a cool proposal:
<br>
<br>=C2=A0 =C2=A0 template &lt;typename T, expression E&gt;
<br>=C2=A0 =C2=A0 T operator||(T p, E e)
<br>=C2=A0 =C2=A0 {
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0if (p)
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return p;
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0return e;
<br>=C2=A0 =C2=A0 }
<br>
<br>=C2=A0 =C2=A0 // ...
<br>=C2=A0 =C2=A0 x =3D x || new int;
<br>
<br>;-)
<br>
<br>Cheers,
<br>Miguel
<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/c7cd5d95-0031-4e50-b7a1-9adcff695bd2%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/c7cd5d95-0031-4e50-b7a1-9adcff695bd2=
%40isocpp.org</a>.<br />

------=_Part_12970_1220829327.1520814514135--

------=_Part_12969_1243461468.1520814514135--

.
