220 37314 <CALvx3hbrrb6tJJYEG7S9q1YrRWSXXi+OCW9n1siXRfFjjxTDYA@mail.gmail.com> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Richard Hodges <hodges.r@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Pointer overload for ||
Date: Mon, 12 Mar 2018 11:42:24 +0100
Lines: 430
Approved: news@gmane.org
Message-ID: <CALvx3hbrrb6tJJYEG7S9q1YrRWSXXi+OCW9n1siXRfFjjxTDYA@mail.gmail.com>
References: <1fea07df-46a6-4df2-865a-849be8f8d129@isocpp.org>
 <CANiq72kL7HQf2oS5THm8bxzurhzoc7uhNQq3S9+zvBce2yMZZQ@mail.gmail.com>
 <c7cd5d95-0031-4e50-b7a1-9adcff695bd2@isocpp.org> <CALvx3hYz84MdBu_WsixPmWf8N=9FY4BiqOv+Lon21nF2m_f0Ow@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="001a1147b7262a52a9056734cce2"
X-Trace: blaine.gmane.org 1520851226 11240 195.159.176.226 (12 Mar 2018 10:40:26 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 12 Mar 2018 10:40:26 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBD4PBM7UWAHRBENTTHKQKGQEGW5ARXA@isocpp.org Mon Mar 12 11:40:21 2018
Return-path: <std-proposals+bncBD4PBM7UWAHRBENTTHKQKGQEGW5ARXA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-io0-f200.google.com ([209.85.223.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBD4PBM7UWAHRBENTTHKQKGQEGW5ARXA@isocpp.org>)
	id 1evKsO-0002mE-18
	for gclcip-std-proposals@m.gmane.org; Mon, 12 Mar 2018 11:40:20 +0100
Original-Received: by mail-io0-f200.google.com with SMTP id 188sf8942496iou.14
        for <gclcip-std-proposals@m.gmane.org>; Mon, 12 Mar 2018 03:42:27 -0700 (PDT)
ARC-Seal: i=2; a=rsa-sha256; t=1520851346; cv=pass;
        d=google.com; s=arc-20160816;
        b=uEmUSGFtlgKwHxZip0j4UqKan6wsEIeRjXAQrrYALeeGn/G5EtGZgCjyZ37idvTF6M
         2S+2cLV2XrmzEJdliSLJ8rdQOjoQtZovDpWX5T5oZw+GTLrc84Z1MpjJBH9r9QeoNkEl
         jgaHTRr/qy5+vdjS2dtoMgtJZIG85ub6ukH1Ovcp0ZkwsAI0QRyFSvdSfvFXPoPhBOdQ
         5Tyo1SOf2CCT5WRkUNCQ8fbYQkdf6QocNwOLDuets+acYhBvZmfFeoYPBHIG9wGUJPGL
         jOqSs6p8GbUAp42lQLLLb15e3Jx35y+PBbq6obHqs80/KF/yvbK7pG6MAW4094kb05Fr
         mr+Q==
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:to:subject:message-id:date
         :from:references:in-reply-to:mime-version:arc-authentication-results
         :arc-message-signature:dkim-signature:arc-authentication-results;
        bh=CIGkeIa4US1F9eqErQj4xtmba7ax4N4nc7LAbM5jZlU=;
        b=bSbqCyWavUuXLYCCiq7z3j8ueZaclFrZY3QqYFkJJGgbUC8fgapYel3xDUSSqcRup8
         F+4EHGkHmfJAF6BB/Muor36lmBF6Sm9sjEwg3L41zZlqZEH4PBZRwZd+Jb4GNTGKQ3lu
         wW4uLdYObysF2OJQB3M/GqxTbtvm1ERMkO8tvEGrWsLDn3lYU5Heff6R2I14g5I7gVd5
         2XlqRAkDCgBE2bJn/RdrEx4B7BhCtc3hBvrM+dsFc8UbHmvP/gWvrtFTn/v6l7swVNaQ
         e7N45wSD722cueKTkIMjy3o86qXJnoD+U5O+YE927lJN73MpGy/pbuOW5ggENPYsTg4B
         i43A==
ARC-Authentication-Results: i=2; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=DKwbZAgP;
       spf=pass (google.com: domain of hodges.r@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hodges.r@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:in-reply-to:references:from:date:message-id:subject:to
         :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=CIGkeIa4US1F9eqErQj4xtmba7ax4N4nc7LAbM5jZlU=;
        b=qarNrSj/0sqDZ/fVHRcrvYwdqjlZFZLygsChzEdTjfiJkUf5s90PLTVvpO4Pt8qX8g
         3LrCnCJb8bqewOCwVqhRd9hlwf05s6LKqq6bV/l5AompAU3dXGD2uwhqh83COxP9uhCi
         EH13cWggd0xHtB6ds0mQiRQJ5PrQcm341hY/QVQC3DlCDmBQGHesd4z0+PCs1BO3IWS2
         jQCb4DmgXZDpSVuSMtw45WxrvnGHE5R6yboS3Rw6o4Px5EaP3UvpUyiz/947y3l+gPKi
         KwV2b44zdeN0UAvaKlzUHjbufyYv+cTh2AVCVYjeATPxlegijTkyOO+m30iT9gG4Wc8b
         gtNQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject: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=CIGkeIa4US1F9eqErQj4xtmba7ax4N4nc7LAbM5jZlU=;
        b=onmAMeekvkPQ7YdPm/XtxT3Bt30WaPTW1FsONT4XOO0YzjBmhYfQVD1trK/i+Kt4EG
         WUVL4N+NsnsH+PZpbULLv0CHopuDUK+zp0dQKTmhx+xYC4YMyLlU8QQMva0lw5xBgyr1
         6m82J4s3fKiKg1VDBIBBXq63opzylb1AZ5moEz5dQzmoG4Q0Upvn9QuZPovZLrFK31eO
         SM/Jerev8xx/hNn4CwWDRClsysPxpQ9Aj+buw5CW/oPCFyeDZB8z2eZnLAnJLc+bvkhm
         f5iirWCDry+QOWhI5hhehZdw6OtSyu0N3d6dsBjiupq2Gc1Qc7mPYN+2EzQnRKDpDcaC
         mG/A==
X-Gm-Message-State: AElRT7GXFZ8GzqWVV4uc+TY+JUZsNMPCGzqrPMOgs9pwOuCSsfeLB1yF
	70eqEH4crMTWyGGiSwfRhyRYIw==
X-Google-Smtp-Source: AG47ELuxmv4lEdeGQQ02HF2yUdQWmDNHSw2XSFZXb3IE1cP3x5sZ+2Ru2GtuJXFyhrzlr7123nhDdg==
X-Received: by 10.36.86.4 with SMTP id o4mr3967797itb.55.1520851346877;
        Mon, 12 Mar 2018 03:42:26 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.81.11 with SMTP id f11ls664001iob.13.gmail; Mon, 12 Mar
 2018 03:42:25 -0700 (PDT)
X-Received: by 10.107.63.68 with SMTP id m65mr8795959ioa.195.1520851345321;
        Mon, 12 Mar 2018 03:42:25 -0700 (PDT)
ARC-Seal: i=1; a=rsa-sha256; t=1520851345; cv=none;
        d=google.com; s=arc-20160816;
        b=LIW7l8nPh7LMe758ujBYIP8sLzDoXVZgQYXXiOznwGutht3wKzG5jnBj/ZnCMMwI3I
         3p91WQbi6f0NkKHj36fNVW6BS7e4kghR9jIWBmHILb7MFNKzux1b12oXw4hUeRZ35Q2V
         +D+WxG+n+079HhCUP0d+DXfj9Fp2a6cvoQL9vRcBQfLSMsSiP4pPhLiiUsBbuYJopSlP
         sWxPEQ5XcOqjADzFJg6uYmh6J/XwPDqAzYI9ULLbv9Tjopgx81OK0mJlce6Zy+DKa7Nm
         19uNjXOhkqF8mu1uYHz+GZf82iSDdNTTD/kyktnVLr9E8tbBd7R3nrr2fnc/Buon41U2
         KgGA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816;
        h=to:subject:message-id:date:from:references:in-reply-to:mime-version
         :dkim-signature:arc-authentication-results;
        bh=9D+wrzqOggCyAF4bgLuvO/gXdUN0Q6nFz84WR/ek3Dw=;
        b=IdEihXKYMGcOS+T+VNMJYpZBh2UA1xx7THiakCRD79QjJD55niYGirrXo1nLWxdYo1
         +2IiEZm0Wp3jSlPEnfLSxbOweFsvI8rzCD0NeR5ZCjmixlIWUNliHTmMpPfect81AAnl
         Kn55tB3QzXREEwMNry+jdYczrFINyMGynz4kJlZOR78/KySTwjD3tMbMsajhExnPBsAN
         jh6rISMVtfgd8cOvzsJu3XnHO1yqda2uObD4fCiHjaK+pQlW+ONV4XXvSF/5dCRTrbG6
         eGzl2qpROZM3S+KLPRghPkICqV7EV6fT03cJZg/k4OyRNrRE5VZdvPQkZ2CHe4hOnX/4
         VSfw==
ARC-Authentication-Results: i=1; mx.google.com;
       dkim=pass header.i=@gmail.com header.s=20161025 header.b=DKwbZAgP;
       spf=pass (google.com: domain of hodges.r@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=hodges.r@gmail.com;
       dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com
Original-Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65])
        by mx.google.com with SMTPS id 129sor2338194ity.85.2018.03.12.03.42.25
        for <std-proposals@isocpp.org>
        (Google Transport Security);
        Mon, 12 Mar 2018 03:42:25 -0700 (PDT)
Received-SPF: pass (google.com: domain of hodges.r@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65;
X-Received: by 10.36.127.10 with SMTP id r10mr8115243itc.101.1520851344837;
 Mon, 12 Mar 2018 03:42:24 -0700 (PDT)
Original-Received: by 10.2.102.105 with HTTP; Mon, 12 Mar 2018 03:42:24 -0700 (PDT)
In-Reply-To: <CALvx3hYz84MdBu_WsixPmWf8N=9FY4BiqOv+Lon21nF2m_f0Ow@mail.gmail.com>
X-Original-Sender: hodges.r@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com header.s=20161025 header.b=DKwbZAgP;       spf=pass
 (google.com: domain of hodges.r@gmail.com designates 209.85.220.65 as
 permitted sender) smtp.mailfrom=hodges.r@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:37314
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/37314>

--001a1147b7262a52a9056734cce2
Content-Type: text/plain; charset="UTF-8"

On 12 March 2018 at 08:33, Richard Hodges <hodges.r@gmail.com> wrote:

> I am a little surprised and dismayed at the eagerness to resort to macros.
>
> We can already safely overload operators with a custom generator type. No
> need for macros, language extensions or any other magic.
>
> live demonstration: http://coliru.stacked-crooked.com/a/bc751df610a220ce
>
>
>
>
However, please don't take my posting "how-to" code as an endorsement.

Having written this, I then implemented a similar concept for unique_ptr (a
little more tricky because of the redundant r-value assignment).

This led me to then look at the "|=" case, but I don't think that makes
semantic sense when applied to pointers:

Consider:

p |= new_thing();

In the above code, do we mean that we wish to replace p with a new thing if
empty, or does it mean that we wish to merge a new thing into the object
referenced by p?


I am inclined to argue that to me it means the latter.





>
>
> On 12 March 2018 at 01:28, <inkwizytoryankes@gmail.com> wrote:
>
>>
>>
>> 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> 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/is
>> ocpp.org/d/msgid/std-proposals/c7cd5d95-0031-4e50-b7a1-
>> 9adcff695bd2%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/c7cd5d95-0031-4e50-b7a1-9adcff695bd2%40isocpp.org?utm_medium=email&utm_source=footer>
>> .
>>
>
>

-- 
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/CALvx3hbrrb6tJJYEG7S9q1YrRWSXXi%2BOCW9n1siXRfFjjxTDYA%40mail.gmail.com.

--001a1147b7262a52a9056734cce2
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><div class=3D"gmail_quo=
te">On 12 March 2018 at 08:33, Richard Hodges <span dir=3D"ltr">&lt;<a href=
=3D"mailto:hodges.r@gmail.com" target=3D"_blank">hodges.r@gmail.com</a>&gt;=
</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">I am a li=
ttle surprised and dismayed at the eagerness to resort to macros.<div><br><=
/div><div>We can already safely overload operators with a custom generator =
type. No need for macros, language extensions or any other magic.</div><div=
><br></div><div>live demonstration:=C2=A0<a href=3D"http://coliru.stacked-c=
rooked.com/a/bc751df610a220ce" target=3D"_blank">http://coliru.<wbr>stacked=
-crooked.com/a/<wbr>bc751df610a220ce</a><br></div><div><br></div><div><br><=
/div><div><br></div></div></blockquote><div><br></div><div>However, please =
don&#39;t take my posting &quot;how-to&quot; code as an endorsement.</div><=
div><br></div><div>Having written this, I then implemented a similar concep=
t for unique_ptr (a little more tricky because of the redundant r-value ass=
ignment).</div><div><br></div><div>This led me to then look at the &quot;|=
=3D&quot; case, but I don&#39;t think that makes semantic sense when applie=
d to pointers:</div><div><br></div><div>Consider:</div><div><br></div><div>=
<font face=3D"monospace, monospace">p |=3D new_thing();=C2=A0 =C2=A0<br></f=
ont></div><div><br></div><div>In the above code, do we mean that we wish to=
 replace p with a new thing if empty, or does it mean that we wish to merge=
 a new thing into the object referenced by p?</div><div><br></div><div><br>=
</div><div>I am inclined to argue that to me it means the latter.</div><div=
><br></div><div><br></div><div><br></div><div>=C2=A0</div><blockquote class=
=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd=
ing-left:1ex"><div dir=3D"ltr"><div></div><div><br></div></div><div class=
=3D"HOEnZb"><div class=3D"h5"><div class=3D"gmail_extra"><br><div class=3D"=
gmail_quote">On 12 March 2018 at 01:28,  <span dir=3D"ltr">&lt;<a href=3D"m=
ailto:inkwizytoryankes@gmail.com" target=3D"_blank">inkwizytoryankes@gmail.=
com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr=
"><br><br>On Sunday, March 11, 2018 at 7:06:59 PM UTC+1, Miguel Ojeda wrote=
:<div><div class=3D"m_-7396058270325159484h5"><blockquote class=3D"gmail_qu=
ote" 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 rel=3D"nofollow">j=
efferson...@gmail.com</a>&gt; wrote:
<br>&gt; Not sure how this will be received -- I haven&#39;t posted here mu=
ch.=C2=A0 Just
<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=A0 They 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=A0 Then 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></div><div><br>This solution is not perfect because =
it reuse `v` multiple times, if value is not simply value it could be probl=
ematic.<br><br>Better would be using lambda similar like in your next point=
:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(1=
87,187,187);border-style:solid;border-width:1px" class=3D"m_-73960582703251=
59484m_8675750317141035639prettyprint"><code class=3D"m_-739605827032515948=
4m_8675750317141035639prettyprint"><div class=3D"m_-7396058270325159484m_86=
75750317141035639subprettyprint"><span style=3D"color:#000" class=3D"m_-739=
6058270325159484m_8675750317141035639styled-by-prettify"><br></span><span s=
tyle=3D"color:#800" class=3D"m_-7396058270325159484m_8675750317141035639sty=
led-by-prettify">#define</span><span style=3D"color:#000" class=3D"m_-73960=
58270325159484m_8675750317141035639styled-by-prettify"> OR_ELSE</span><span=
 style=3D"color:#660" class=3D"m_-7396058270325159484m_8675750317141035639s=
tyled-by-prettify">(</span><span style=3D"color:#000" class=3D"m_-739605827=
0325159484m_8675750317141035639styled-by-prettify">v</span><span style=3D"c=
olor:#660" class=3D"m_-7396058270325159484m_8675750317141035639styled-by-pr=
ettify">,</span><span style=3D"color:#000" class=3D"m_-7396058270325159484m=
_8675750317141035639styled-by-prettify"> e</span><span style=3D"color:#660"=
 class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">)<=
/span><span style=3D"color:#000" class=3D"m_-7396058270325159484m_867575031=
7141035639styled-by-prettify"> </span><span style=3D"color:#660" class=3D"m=
_-7396058270325159484m_8675750317141035639styled-by-prettify">([&amp;](</sp=
an><span style=3D"color:#008" class=3D"m_-7396058270325159484m_867575031714=
1035639styled-by-prettify">auto</span><span style=3D"color:#000" class=3D"m=
_-7396058270325159484m_8675750317141035639styled-by-prettify"> MACRO_OR_ELS=
E_VALUE_X</span><span style=3D"color:#660" class=3D"m_-7396058270325159484m=
_8675750317141035639styled-by-prettify">){</span><span style=3D"color:#000"=
 class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify"> <=
/span><span style=3D"color:#008" class=3D"m_-7396058270325159484m_867575031=
7141035639styled-by-prettify">return</span><span style=3D"color:#000" class=
=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify"> </span>=
<span style=3D"color:#660" class=3D"m_-7396058270325159484m_867575031714103=
5639styled-by-prettify">(</span><span style=3D"color:#000" class=3D"m_-7396=
058270325159484m_8675750317141035639styled-by-prettify">MACRO_OR_ELSE_VALUE=
_X </span><span style=3D"color:#660" class=3D"m_-7396058270325159484m_86757=
50317141035639styled-by-prettify">?</span><span style=3D"color:#000" class=
=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify"> MACRO_O=
R_ELSE_VALUE_X </span><span style=3D"color:#660" class=3D"m_-73960582703251=
59484m_8675750317141035639styled-by-prettify">:</span><span style=3D"color:=
#000" class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettif=
y"> </span><span style=3D"color:#660" class=3D"m_-7396058270325159484m_8675=
750317141035639styled-by-prettify">(</span><span style=3D"color:#000" class=
=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">e</span>=
<span style=3D"color:#660" class=3D"m_-7396058270325159484m_867575031714103=
5639styled-by-prettify">));</span><span style=3D"color:#000" class=3D"m_-73=
96058270325159484m_8675750317141035639styled-by-prettify"> </span><span sty=
le=3D"color:#660" class=3D"m_-7396058270325159484m_8675750317141035639style=
d-by-prettify">}(</span><span style=3D"color:#000" class=3D"m_-739605827032=
5159484m_8675750317141035639styled-by-prettify">v</span><span style=3D"colo=
r:#660" class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prett=
ify">))</span><span style=3D"color:#000" class=3D"m_-7396058270325159484m_8=
675750317141035639styled-by-prettify"><br><br></span></div></code></div><br=
><br>This will allow evaluate `v` only once and can be inserted in other ex=
pressions:<br><br><div style=3D"background-color:rgb(250,250,250);border-co=
lor:rgb(187,187,187);border-style:solid;border-width:1px" class=3D"m_-73960=
58270325159484m_8675750317141035639prettyprint"><code class=3D"m_-739605827=
0325159484m_8675750317141035639prettyprint"><div class=3D"m_-73960582703251=
59484m_8675750317141035639subprettyprint"><span style=3D"color:#008" class=
=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">auto</sp=
an><span style=3D"color:#000" class=3D"m_-7396058270325159484m_867575031714=
1035639styled-by-prettify"> z </span><span style=3D"color:#660" class=3D"m_=
-7396058270325159484m_8675750317141035639styled-by-prettify">=3D</span><spa=
n style=3D"color:#000" class=3D"m_-7396058270325159484m_8675750317141035639=
styled-by-prettify"> h</span><span style=3D"color:#660" class=3D"m_-7396058=
270325159484m_8675750317141035639styled-by-prettify">()</span><span style=
=3D"color:#000" class=3D"m_-7396058270325159484m_8675750317141035639styled-=
by-prettify"> </span><span style=3D"color:#660" class=3D"m_-739605827032515=
9484m_8675750317141035639styled-by-prettify">?</span><span style=3D"color:#=
000" class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify=
"> OR_ELSE</span><span style=3D"color:#660" class=3D"m_-7396058270325159484=
m_8675750317141035639styled-by-prettify">(</span><span style=3D"color:#000"=
 class=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">OR=
_ELSE</span><span style=3D"color:#660" class=3D"m_-7396058270325159484m_867=
5750317141035639styled-by-prettify">(</span><span style=3D"color:#000" clas=
s=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">x</span=
><span style=3D"color:#660" class=3D"m_-7396058270325159484m_86757503171410=
35639styled-by-prettify">,</span><span style=3D"color:#000" class=3D"m_-739=
6058270325159484m_8675750317141035639styled-by-prettify"> foo</span><span s=
tyle=3D"color:#660" class=3D"m_-7396058270325159484m_8675750317141035639sty=
led-by-prettify">()),</span><span style=3D"color:#000" class=3D"m_-73960582=
70325159484m_8675750317141035639styled-by-prettify"> bar</span><span style=
=3D"color:#660" class=3D"m_-7396058270325159484m_8675750317141035639styled-=
by-prettify">())</span><span style=3D"color:#000" class=3D"m_-7396058270325=
159484m_8675750317141035639styled-by-prettify"> </span><span style=3D"color=
:#660" class=3D"m_-7396058270325159484m_8675750317141035639styled-by-pretti=
fy">:</span><span style=3D"color:#000" class=3D"m_-7396058270325159484m_867=
5750317141035639styled-by-prettify"> zz</span><span style=3D"color:#660" cl=
ass=3D"m_-7396058270325159484m_8675750317141035639styled-by-prettify">();</=
span><span style=3D"color:#000" class=3D"m_-7396058270325159484m_8675750317=
141035639styled-by-prettify"><br></span></div></code></div><br><br>=C2=A0</=
div><div><div class=3D"m_-7396058270325159484h5"><blockquote class=3D"gmail=
_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padd=
ing-left:1ex">If you are really keen on having the operator, you could alwa=
ys
<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></div></div><div><div class=3D"m_-739605827032515948=
4h5">

<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" target=3D"_=
blank">std-proposals+unsubscribe@isoc<wbr>pp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br></div></div>
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&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/is<wbr>ocpp.org/d/msgid/std-proposals<wbr>/c7cd=
5d95-0031-4e50-b7a1-<wbr>9adcff695bd2%40isocpp.org</a>.<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div></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/CALvx3hbrrb6tJJYEG7S9q1YrRWSXXi%2BOCW=
9n1siXRfFjjxTDYA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hbrrb6tJJ=
YEG7S9q1YrRWSXXi%2BOCW9n1siXRfFjjxTDYA%40mail.gmail.com</a>.<br />

--001a1147b7262a52a9056734cce2--

.
