Topic: Pointer overload for ||
Author: jeffersoncarpenter2@gmail.com
Date: Sun, 11 Mar 2018 00:38:17 -0800 (PST)
Raw View
------=_Part_4686_264317583.1520757497056
Content-Type: multipart/alternative;
boundary="----=_Part_4687_1570609456.1520757497056"
------=_Part_4687_1570609456.1520757497056
Content-Type: text/plain; charset="UTF-8"
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;
}
would be nice.
Currently, if I am not mistaken, || (excluding user-defined overloads) is a
boolean-only operator. It is indeed the case that nullptr is the only
value that becomes false when cast to a boolean, and all other pointer
values become true. So I don't see how adding an || overload for pointers
would break any existing code.
The main thing that would make this a no-go would be if the semantics of ||
required that both operands be evaluated -- whether or not the left-hand
one is false. Then the whole point as I see it would be lost - the above
examples would merely leak memory.
- Jefferson Carpenter
--
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/1fea07df-46a6-4df2-865a-849be8f8d129%40isocpp.org.
------=_Part_4687_1570609456.1520757497056
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Not sure how this will be received -- I haven't posted=
here much.=C2=A0 Just thought it would be worth mentioning this idea.<br><=
br>Suppose you have an API that you want to optionally allocate storage for=
your user.=C2=A0 They can either pass in a pointer to some existing alloca=
ted space, or pass in a null pointer and space is allocated for them.<br><b=
r>void foo(int *x) {<br>=C2=A0 if (x =3D=3D nullptr) {<br>=C2=A0=C2=A0=C2=
=A0 x =3D new int;<br>=C2=A0 }<br>=C2=A0 // etc.<br>}<br><br>Just figured i=
t would be a decent idea if the || operator received a pointer overload.=C2=
=A0 Then you could write the more succinct<br><br>void foo(int *x) {<br>=C2=
=A0 x =3D x || new int;<br>}<br><br>or even (ducks for cover)<br><br>void f=
oo(int *x) {<br>=C2=A0 x ||=3D new int;<br>}<br><br>would be nice.<br><br>C=
urrently, if I am not mistaken, || (excluding user-defined overloads) is a =
boolean-only operator.=C2=A0 It is indeed the case that nullptr is the only=
value that becomes false when cast to a boolean, and all other pointer val=
ues become true.=C2=A0 So I don't see how adding an || overload for poi=
nters would break any existing code.<br><br>The main thing that would make =
this a no-go would be if the semantics of || required that both operands be=
evaluated -- whether or not the left-hand one is false.=C2=A0 Then the who=
le point as I see it would be lost - the above examples would merely leak m=
emory.<br><br>- Jefferson Carpenter<br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/1fea07df-46a6-4df2-865a-849be8f8d129%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1fea07df-46a6-4df2-865a-849be8f8d129=
%40isocpp.org</a>.<br />
------=_Part_4687_1570609456.1520757497056--
------=_Part_4686_264317583.1520757497056--
.
Author: =?UTF-8?B?R2HFoXBlciBBxb5tYW4=?= <gasper.azman@gmail.com>
Date: Sun, 11 Mar 2018 08:58:46 +0000
Raw View
--94eb2c11d754e1238605671f3c11
Content-Type: text/plain; charset="UTF-8"
There is a binary form of the conditional operator that is a gnu extension:
void foo(int *x) {
x = x?:new int;
}
It's defined as
CEXPR ?: FEXPR;
and rewritten into
{
auto&& x = CEXPR;
if (x) { return x; } else { return FEXPR; }
}
Perhaps you could propose that instead, as it's far more general. It's a
GNU extension because certain macros (assert comes to mind) are much easier
to implement if you have it.
G
On Sun, Mar 11, 2018, 08:38 <jeffersoncarpenter2@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;
> }
>
> would be nice.
>
> Currently, if I am not mistaken, || (excluding user-defined overloads) is
> a boolean-only operator. It is indeed the case that nullptr is the only
> value that becomes false when cast to a boolean, and all other pointer
> values become true. So I don't see how adding an || overload for pointers
> would break any existing code.
>
> The main thing that would make this a no-go would be if the semantics of
> || required that both operands be evaluated -- whether or not the left-hand
> one is false. Then the whole point as I see it would be lost - the above
> examples would merely leak memory.
>
> - Jefferson Carpenter
>
> --
> 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/1fea07df-46a6-4df2-
> 865a-849be8f8d129%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1fea07df-46a6-4df2-865a-849be8f8d129%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/CAANG%3DkVsGNaXGZLtbNm%3D9cJxjLXwu0cgL%3Dp_kYa1TCRSKfNCvg%40mail.gmail.com.
--94eb2c11d754e1238605671f3c11
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div dir=3D"auto"><div>There is a binary form of the condi=
tional operator that is a gnu extension:</div><div><br></div><div>void foo(=
int *x) {</div><div>=C2=A0 x =3D x?:new int;</div><div>}</div><div><br></di=
v><div>It's defined as</div><div><br></div><div>CEXPR ?: FEXPR;</div><d=
iv><br></div><div>and rewritten into</div><div><br></div><div>{</div><div>=
=C2=A0 =C2=A0auto&& x =3D CEXPR;</div><div>=C2=A0 =C2=A0if (x) { re=
turn x; } else { return FEXPR; }</div><div>}</div><div><br></div><div>Perha=
ps you could propose that instead, as it's far more general. It's a=
GNU extension because certain macros (assert comes to mind) are much easie=
r to implement if you have it.</div><div><br></div><div>G</div><div><div di=
r=3D"auto"><br></div><div dir=3D"auto"><br></div><br><br><div class=3D"gmai=
l_quote"><div dir=3D"ltr">On Sun, Mar 11, 2018, 08:38 <<a href=3D"mailt=
o:jeffersoncarpenter2@gmail.com" target=3D"_blank">jeffersoncarpenter2@gmai=
l.com</a><wbr>> wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=
=3D"ltr">Not sure how this will be received -- I haven't posted here mu=
ch.=C2=A0 Just thought it would be worth mentioning this idea.<br><br>Suppo=
se you have an API that you want to optionally allocate storage for your us=
er.=C2=A0 They can either pass in a pointer to some existing allocated spac=
e, or pass in a null pointer and space is allocated for them.<br><br>void f=
oo(int *x) {<br>=C2=A0 if (x =3D=3D nullptr) {<br>=C2=A0=C2=A0=C2=A0 x =3D =
new int;<br>=C2=A0 }<br>=C2=A0 // etc.<br>}<br><br>Just figured it would be=
a decent idea if the || operator received a pointer overload.=C2=A0 Then y=
ou could write the more succinct<br><br>void foo(int *x) {<br>=C2=A0 x =3D =
x || new int;<br>}<br><br>or even (ducks for cover)<br><br>void foo(int *x)=
{<br>=C2=A0 x ||=3D new int;<br>}<br><br>would be nice.<br><br>Currently, =
if I am not mistaken, || (excluding user-defined overloads) is a boolean-on=
ly operator.=C2=A0 It is indeed the case that nullptr is the only value tha=
t becomes false when cast to a boolean, and all other pointer values become=
true.=C2=A0 So I don't see how adding an || overload for pointers woul=
d break any existing code.<br><br>The main thing that would make this a no-=
go would be if the semantics of || required that both operands be evaluated=
-- whether or not the left-hand one is false.=C2=A0 Then the whole point a=
s I see it would be lost - the above examples would merely leak memory.<br>=
<br>- Jefferson Carpenter<br></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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" rel=3D"nore=
ferrer" target=3D"_blank">std-proposals+unsubscribe@<wbr>isocpp.org</a>.<br=
>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" rel=3D"noreferrer" target=3D"_blank">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/1fea07df-46a6-4df2-865a-849be8f8d129%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"noreferrer"=
target=3D"_blank">https://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-=
<wbr>proposals/1fea07df-46a6-4df2-<wbr>865a-849be8f8d129%40isocpp.org</a><w=
br>.<br>
</blockquote></div></div></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/CAANG%3DkVsGNaXGZLtbNm%3D9cJxjLXwu0cg=
L%3Dp_kYa1TCRSKfNCvg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAANG%3DkVs=
GNaXGZLtbNm%3D9cJxjLXwu0cgL%3Dp_kYa1TCRSKfNCvg%40mail.gmail.com</a>.<br />
--94eb2c11d754e1238605671f3c11--
.
Author: Jefferson Carpenter <jeffersoncarpenter2@gmail.com>
Date: Sun, 11 Mar 2018 04:01:41 -0500
Raw View
Indeed, that's very nice.
On 3/11/2018 3:58 AM, Ga=C5=A1per A=C5=BEman wrote:
> There is a binary form of the conditional operator that is a gnu extensio=
n:
>=20
> void foo(int *x) {
> x =3D x?:new int;
> }
>=20
> It's defined as
>=20
> CEXPR ?: FEXPR;
>=20
> and rewritten into
>=20
> {
> auto&& x =3D CEXPR;
> if (x) { return x; } else { return FEXPR; }
> }
>=20
> Perhaps you could propose that instead, as it's far more general. It's a
> GNU extension because certain macros (assert comes to mind) are much easi=
er
> to implement if you have it.
>=20
> G
>=20
>=20
>=20
>=20
> On Sun, Mar 11, 2018, 08:38 <jeffersoncarpenter2@gmail.com> wrote:
>=20
>> 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 =3D=3D nullptr) {
>> x =3D 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 =3D x || new int;
>> }
>>
>> or even (ducks for cover)
>>
>> void foo(int *x) {
>> x ||=3D new int;
>> }
>>
>> would be nice.
>>
>> Currently, if I am not mistaken, || (excluding user-defined overloads) i=
s
>> a boolean-only operator. It is indeed the case that nullptr is the only
>> value that becomes false when cast to a boolean, and all other pointer
>> values become true. So I don't see how adding an || overload for pointe=
rs
>> would break any existing code.
>>
>> The main thing that would make this a no-go would be if the semantics of
>> || required that both operands be evaluated -- whether or not the left-h=
and
>> one is false. Then the whole point as I see it would be lost - the abov=
e
>> examples would merely leak memory.
>>
>> - Jefferson Carpenter
>>
>> --
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> 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/1fea07df-46a6-4df2-
>> 865a-849be8f8d129%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1fea07df-4=
6a6-4df2-865a-849be8f8d129%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoo=
ter>
>> .
>>
>=20
--=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/f2c867ca-f34f-e3ad-1f3e-b8aabc8e13b0%40gmail.com=
..
.
Author: =?UTF-8?B?R2HFoXBlciBBxb5tYW4=?= <gasper.azman@gmail.com>
Date: Sun, 11 Mar 2018 09:02:16 +0000
Raw View
--0000000000006fca9705671f4913
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
In my morning fog I forgot I needed to specify that it was the version of
assert where you only evaluate the message portion if the condition failed.
Not the standard assert.
On Sun, Mar 11, 2018 at 8:58 AM, Ga=C5=A1per A=C5=BEman <gasper.azman@gmail=
..com>
wrote:
> There is a binary form of the conditional operator that is a gnu extensio=
n:
>
> void foo(int *x) {
> x =3D x?:new int;
> }
>
> It's defined as
>
> CEXPR ?: FEXPR;
>
> and rewritten into
>
> {
> auto&& x =3D CEXPR;
> if (x) { return x; } else { return FEXPR; }
> }
>
> Perhaps you could propose that instead, as it's far more general. It's a
> GNU extension because certain macros (assert comes to mind) are much easi=
er
> to implement if you have it.
>
> G
>
>
>
>
> On Sun, Mar 11, 2018, 08:38 <jeffersoncarpenter2@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 =3D=3D nullptr) {
>> x =3D 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 =3D x || new int;
>> }
>>
>> or even (ducks for cover)
>>
>> void foo(int *x) {
>> x ||=3D new int;
>> }
>>
>> would be nice.
>>
>> Currently, if I am not mistaken, || (excluding user-defined overloads) i=
s
>> a boolean-only operator. It is indeed the case that nullptr is the only
>> value that becomes false when cast to a boolean, and all other pointer
>> values become true. So I don't see how adding an || overload for pointe=
rs
>> would break any existing code.
>>
>> The main thing that would make this a no-go would be if the semantics of
>> || required that both operands be evaluated -- whether or not the left-h=
and
>> one is false. Then the whole point as I see it would be lost - the abov=
e
>> examples would merely leak memory.
>>
>> - Jefferson Carpenter
>>
>> --
>> You received this message because you are subscribed to the Google Group=
s
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n
>> 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/1fea07df-46a6-4df2-865a-
>> 849be8f8d129%40isocpp.org
>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1fea07df-4=
6a6-4df2-865a-849be8f8d129%40isocpp.org?utm_medium=3Demail&utm_source=3Dfoo=
ter>
>> .
>>
>
--=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/CAANG%3DkUmThSamFisncxMCaqFd6Ze7GWH-q%2BTjMn4YfF=
Tn2T8%2Bw%40mail.gmail.com.
--0000000000006fca9705671f4913
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">In my morning fog I forgot I needed to specify that it was=
the version of assert where you only evaluate the message portion if the c=
ondition failed. Not the standard assert.</div><div class=3D"gmail_extra"><=
br><div class=3D"gmail_quote">On Sun, Mar 11, 2018 at 8:58 AM, Ga=C5=A1per =
A=C5=BEman <span dir=3D"ltr"><<a href=3D"mailto:gasper.azman@gmail.com" =
target=3D"_blank">gasper.azman@gmail.com</a>></span> wrote:<br><blockquo=
te class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc so=
lid;padding-left:1ex"><div dir=3D"ltr"><div dir=3D"auto"><div>There is a bi=
nary form of the conditional operator that is a gnu extension:</div><div><b=
r></div><div>void foo(int *x) {</div><div>=C2=A0 x =3D x?:new int;</div><di=
v>}</div><div><br></div><div>It's defined as</div><div><br></div><div>C=
EXPR ?: FEXPR;</div><div><br></div><div>and rewritten into</div><div><br></=
div><div>{</div><div>=C2=A0 =C2=A0auto&& x =3D CEXPR;</div><div>=C2=
=A0 =C2=A0if (x) { return x; } else { return FEXPR; }</div><div>}</div><div=
><br></div><div>Perhaps you could propose that instead, as it's far mor=
e general. It's a GNU extension because certain macros (assert comes to=
mind) are much easier to implement if you have it.</div><span class=3D"HOE=
nZb"><font color=3D"#888888"><div><br></div><div>G</div></font></span><div>=
<div class=3D"h5"><div><div dir=3D"auto"><br></div><div dir=3D"auto"><br></=
div><br><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, Mar 11, 201=
8, 08:38 <<a href=3D"mailto:jeffersoncarpenter2@gmail.com" target=3D"_b=
lank">jeffersoncarpenter2@gmail.com</a><wbr>> wrote:<br></div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex"><div dir=3D"ltr">Not sure how this will be received --=
I haven't posted here much.=C2=A0 Just thought it would be worth menti=
oning this idea.<br><br>Suppose you have an API that you want to optionally=
allocate storage for your user.=C2=A0 They can either pass in a pointer to=
some existing allocated space, or pass in a null pointer and space is allo=
cated for them.<br><br>void foo(int *x) {<br>=C2=A0 if (x =3D=3D nullptr) {=
<br>=C2=A0=C2=A0=C2=A0 x =3D new int;<br>=C2=A0 }<br>=C2=A0 // etc.<br>}<br=
><br>Just figured it would be a decent idea if the || operator received a p=
ointer overload.=C2=A0 Then you could write the more succinct<br><br>void f=
oo(int *x) {<br>=C2=A0 x =3D x || new int;<br>}<br><br>or even (ducks for c=
over)<br><br>void foo(int *x) {<br>=C2=A0 x ||=3D new int;<br>}<br><br>woul=
d be nice.<br><br>Currently, if I am not mistaken, || (excluding user-defin=
ed overloads) is a boolean-only operator.=C2=A0 It is indeed the case that =
nullptr is the only value that becomes false when cast to a boolean, and al=
l other pointer values become true.=C2=A0 So I don't see how adding an =
|| overload for pointers would break any existing code.<br><br>The main thi=
ng that would make this a no-go would be if the semantics of || required th=
at both operands be evaluated -- whether or not the left-hand one is false.=
=C2=A0 Then the whole point as I see it would be lost - the above examples =
would merely leak memory.<br><br>- Jefferson Carpenter<br></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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" rel=3D"nore=
ferrer" 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" rel=3D"noreferrer" target=3D"_blank">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/1fea07df-46a6-4df2-865a-849be8f8d129%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"noreferrer"=
target=3D"_blank">https://groups.google.com/a/is<wbr>ocpp.org/d/msgid/std-=
proposals<wbr>/1fea07df-46a6-4df2-865a-<wbr>849be8f8d129%40isocpp.org</a>.<=
br>
</blockquote></div></div></div></div></div></div>
</blockquote></div><br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/CAANG%3DkUmThSamFisncxMCaqFd6Ze7GWH-q=
%2BTjMn4YfFTn2T8%2Bw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAANG%3DkUm=
ThSamFisncxMCaqFd6Ze7GWH-q%2BTjMn4YfFTn2T8%2Bw%40mail.gmail.com</a>.<br />
--0000000000006fca9705671f4913--
.
Author: Nicolas Lesser <blitzrakete@gmail.com>
Date: Sun, 11 Mar 2018 10:16:56 +0100
Raw View
--f4f5e808dde8ad2a8c05671f7c7a
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
I don't see the point. How is your if not succinct?
if (!ptr)
ptr =3D new int;
is very short and understandable. More so than your idea, as that
complicates the logical or operator.
Also that inverted logic has a high chance of confusing anyone who comes
across it, and it some cases, it is not even clear which or operator you
choose.
var =3D var || allocate();
If allocate doesn't return a pointer, but a boolean (or due to a refactor
it changes) then the expression above would no longer do the right thing:
assign 1 to var if either expression is true, which is not expected.
You could take inspiration from Python or use the GNU binary op Ga=C5=A1per
mentioned:
x =3D new int if (!x);
But I don't like that either. In short, the first if version is by far the
most understandable and clear way of achieving what you want IMO.
On Mar 11, 2018 10:03 AM, "Ga=C5=A1per A=C5=BEman" <gasper.azman@gmail.com>=
wrote:
> In my morning fog I forgot I needed to specify that it was the version of
> assert where you only evaluate the message portion if the condition faile=
d.
> Not the standard assert.
>
> On Sun, Mar 11, 2018 at 8:58 AM, Ga=C5=A1per A=C5=BEman <gasper.azman@gma=
il.com>
> wrote:
>
>> There is a binary form of the conditional operator that is a gnu
>> extension:
>>
>> void foo(int *x) {
>> x =3D x?:new int;
>> }
>>
>> It's defined as
>>
>> CEXPR ?: FEXPR;
>>
>> and rewritten into
>>
>> {
>> auto&& x =3D CEXPR;
>> if (x) { return x; } else { return FEXPR; }
>> }
>>
>> Perhaps you could propose that instead, as it's far more general. It's a
>> GNU extension because certain macros (assert comes to mind) are much eas=
ier
>> to implement if you have it.
>>
>> G
>>
>>
>>
>>
>> On Sun, Mar 11, 2018, 08:38 <jeffersoncarpenter2@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 fo=
r
>>> your user. They can either pass in a pointer to some existing allocate=
d
>>> space, or pass in a null pointer and space is allocated for them.
>>>
>>> void foo(int *x) {
>>> if (x =3D=3D nullptr) {
>>> x =3D 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 =3D x || new int;
>>> }
>>>
>>> or even (ducks for cover)
>>>
>>> void foo(int *x) {
>>> x ||=3D new int;
>>> }
>>>
>>> would be nice.
>>>
>>> Currently, if I am not mistaken, || (excluding user-defined overloads)
>>> is a boolean-only operator. It is indeed the case that nullptr is the =
only
>>> value that becomes false when cast to a boolean, and all other pointer
>>> values become true. So I don't see how adding an || overload for point=
ers
>>> would break any existing code.
>>>
>>> The main thing that would make this a no-go would be if the semantics o=
f
>>> || required that both operands be evaluated -- whether or not the left-=
hand
>>> one is false. Then the whole point as I see it would be lost - the abo=
ve
>>> examples would merely leak memory.
>>>
>>> - Jefferson Carpenter
>>>
>>> --
>>> 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/1fea07df-46a6-4df2-865a-849be
>>> 8f8d129%40isocpp.org
>>> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1fea07df-=
46a6-4df2-865a-849be8f8d129%40isocpp.org?utm_medium=3Demail&utm_source=3Dfo=
oter>
>>> .
>>>
>>
> --
> 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/CAANG%3DkUmThSamFisncxMCaqFd6Ze7GWH-
> q%2BTjMn4YfFTn2T8%2Bw%40mail.gmail.com
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAANG%3DkUm=
ThSamFisncxMCaqFd6Ze7GWH-q%2BTjMn4YfFTn2T8%2Bw%40mail.gmail.com?utm_medium=
=3Demail&utm_source=3Dfooter>
> .
>
--=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/CALmDwq3powZ_ytSPztv%3DZz-TryE1jcDwDsDhtoABggvcF=
eJK%2Bg%40mail.gmail.com.
--f4f5e808dde8ad2a8c05671f7c7a
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto">I don't see the point. How is your if not succinct?<d=
iv dir=3D"auto"><br></div><div dir=3D"auto">if (!ptr)</div><div dir=3D"auto=
">=C2=A0 =C2=A0 ptr =3D new int;</div><div dir=3D"auto"><br></div><div dir=
=3D"auto">is very short and understandable. More so than your idea, as that=
complicates the logical or operator.</div><div dir=3D"auto"><br></div><div=
dir=3D"auto">Also that inverted logic has a high chance of confusing anyon=
e who comes across it, and it some cases, it is not even clear which or ope=
rator you choose.</div><div dir=3D"auto"><br></div><div dir=3D"auto">var =
=3D var || allocate();</div><div dir=3D"auto"><br></div><div dir=3D"auto">I=
f allocate doesn't return a pointer, but a boolean (or due to a refacto=
r it changes) then the expression above would no longer do the right thing:=
assign 1 to var if either expression is true, which is not expected.</div>=
<div dir=3D"auto"><br></div><div dir=3D"auto">You could take inspiration fr=
om Python or use the GNU binary op Ga=C5=A1per mentioned:</div><div dir=3D"=
auto"><br></div><div dir=3D"auto">x =3D new int if (!x);</div><div dir=3D"a=
uto"><br></div><div dir=3D"auto">But I don't like that either. In short=
, the first if version is by far the most understandable and clear way of a=
chieving what you want IMO.</div></div><div class=3D"gmail_extra"><br><div =
class=3D"gmail_quote">On Mar 11, 2018 10:03 AM, "Ga=C5=A1per A=C5=BEma=
n" <<a href=3D"mailto:gasper.azman@gmail.com" target=3D"_blank">gas=
per.azman@gmail.com</a>> wrote:<br type=3D"attribution"><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex"><div dir=3D"ltr">In my morning fog I forgot I needed to spec=
ify that it was the version of assert where you only evaluate the message p=
ortion if the condition failed. Not the standard assert.</div><div class=3D=
"gmail_extra"><br><div class=3D"gmail_quote">On Sun, Mar 11, 2018 at 8:58 A=
M, Ga=C5=A1per A=C5=BEman <span dir=3D"ltr"><<a href=3D"mailto:gasper.az=
man@gmail.com" target=3D"_blank">gasper.azman@gmail.com</a>></span> wrot=
e:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l=
eft:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div dir=3D"auto"><di=
v>There is a binary form of the conditional operator that is a gnu extensio=
n:</div><div><br></div><div>void foo(int *x) {</div><div>=C2=A0 x =3D x?:ne=
w int;</div><div>}</div><div><br></div><div>It's defined as</div><div><=
br></div><div>CEXPR ?: FEXPR;</div><div><br></div><div>and rewritten into</=
div><div><br></div><div>{</div><div>=C2=A0 =C2=A0auto&& x =3D CEXPR=
;</div><div>=C2=A0 =C2=A0if (x) { return x; } else { return FEXPR; }</div><=
div>}</div><div><br></div><div>Perhaps you could propose that instead, as i=
t's far more general. It's a GNU extension because certain macros (=
assert comes to mind) are much easier to implement if you have it.</div><sp=
an class=3D"m_4872259237021468760HOEnZb"><font color=3D"#888888"><div><br><=
/div><div>G</div></font></span><div><div class=3D"m_4872259237021468760h5">=
<div><div dir=3D"auto"><br></div><div dir=3D"auto"><br></div><br><br><div c=
lass=3D"gmail_quote"><div dir=3D"ltr">On Sun, Mar 11, 2018, 08:38 <<a h=
ref=3D"mailto:jeffersoncarpenter2@gmail.com" target=3D"_blank">jeffersoncar=
penter2@gmail.com</a><wbr>> wrote:<br></div><blockquote class=3D"gmail_q=
uote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1e=
x"><div dir=3D"ltr">Not sure how this will be received -- I haven't pos=
ted here much.=C2=A0 Just thought it would be worth mentioning this idea.<b=
r><br>Suppose you have an API that you want to optionally allocate storage =
for your user.=C2=A0 They can either pass in a pointer to some existing all=
ocated space, or pass in a null pointer and space is allocated for them.<br=
><br>void foo(int *x) {<br>=C2=A0 if (x =3D=3D nullptr) {<br>=C2=A0=C2=A0=
=C2=A0 x =3D new int;<br>=C2=A0 }<br>=C2=A0 // etc.<br>}<br><br>Just figure=
d it would be a decent idea if the || operator received a pointer overload.=
=C2=A0 Then you could write the more succinct<br><br>void foo(int *x) {<br>=
=C2=A0 x =3D x || new int;<br>}<br><br>or even (ducks for cover)<br><br>voi=
d foo(int *x) {<br>=C2=A0 x ||=3D new int;<br>}<br><br>would be nice.<br><b=
r>Currently, if I am not mistaken, || (excluding user-defined overloads) is=
a boolean-only operator.=C2=A0 It is indeed the case that nullptr is the o=
nly value that becomes false when cast to a boolean, and all other pointer =
values become true.=C2=A0 So I don't see how adding an || overload for =
pointers would break any existing code.<br><br>The main thing that would ma=
ke this a no-go would be if the semantics of || required that both operands=
be evaluated -- whether or not the left-hand one is false.=C2=A0 Then the =
whole point as I see it would be lost - the above examples would merely lea=
k memory.<br><br>- Jefferson Carpenter<br></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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" rel=3D"nore=
ferrer" 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" rel=3D"noreferrer" target=3D"_blank">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/1fea07df-46a6-4df2-865a-849be8f8d129%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"noreferrer"=
target=3D"_blank">https://groups.google.com/a/is<wbr>ocpp.org/d/msgid/std-=
proposals<wbr>/1fea07df-46a6-4df2-865a-849be<wbr>8f8d129%40isocpp.org</a>.<=
br>
</blockquote></div></div></div></div></div></div>
</blockquote></div><br></div>
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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" target=3D"_=
blank">std-proposals+unsubscribe@<wbr>isocpp.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>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAANG%3DkUmThSamFisncxMCaqFd6Ze7GWH-q=
%2BTjMn4YfFTn2T8%2Bw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Df=
ooter" target=3D"_blank">https://groups.google.com/a/<wbr>isocpp.org/d/msgi=
d/std-<wbr>proposals/CAANG%<wbr>3DkUmThSamFisncxMCaqFd6Ze7GWH-<wbr>q%2BTjMn=
4YfFTn2T8%2Bw%40mail.<wbr>gmail.com</a>.<br>
</blockquote></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/CALmDwq3powZ_ytSPztv%3DZz-TryE1jcDwDs=
DhtoABggvcFeJK%2Bg%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq3powZ_=
ytSPztv%3DZz-TryE1jcDwDsDhtoABggvcFeJK%2Bg%40mail.gmail.com</a>.<br />
--f4f5e808dde8ad2a8c05671f7c7a--
.
Author: Jefferson Carpenter <jeffersoncarpenter2@gmail.com>
Date: Sun, 11 Mar 2018 04:40:05 -0500
Raw View
On 3/11/2018 4:16 AM, Nicolas Lesser wrote:
> I don't see the point. How is your if not succinct?
>
> if (!ptr)
> ptr = new int;
Yeah, I guess the if statement is good enough. I agree, there's no need
to add the python- or ruby-like form of the if statement.
BTW the only reason I was hesitant to use the ternary operator, "ptr =
ptr ? ptr : new int", is just because you would need to write ptr three
times, yuck.
However, the ternary operator does have the simple advantage over the if
statement that you can use it at the expression level.
> Also that inverted logic has a high chance of confusing anyone who comes
> across it, and it some cases, it is not even clear which or operator you
> choose.
>
> var = var || allocate();
>
> If allocate doesn't return a pointer, but a boolean (or due to a refactor
> it changes) then the expression above would no longer do the right thing:
> assign 1 to var if either expression is true, which is not expected.
I don't know precisely what you mean by "inverted" logic, but I don't
agree that it would necessarily confuse anyone who comes across it - it
would just be unfamiliar syntax, which they would look up. It's true
that the expression would compile regardless of whether allocate()
returns a bool or a pointer, which could cause a surprising bug due to
implicit pointer-to-bool conversion, but I don't feel like that's very
likely, or that it would be very likely that a refactor would cause a
problem there either. Specifically if your allocate() is changed from
returning a pointer to returning a bool during a refactor, its signature
would change in other ways that would cause a compile error there, i.e.
taking a reference to the pointer to allocate as an argument instead of
returning it.
Jefferson Carpenter
--
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/05da71c2-80cf-63dd-b2c6-8cd342db5140%40gmail.com.
.
Author: Nicolas Lesser <blitzrakete@gmail.com>
Date: Sun, 11 Mar 2018 11:00:38 +0100
Raw View
--089e08e5c2e9f08919056720183d
Content-Type: text/plain; charset="UTF-8"
> Also that inverted logic has a high chance of confusing anyone who comes
> across it, and it some cases, it is not even clear which or operator you
> choose.
>
> var = var || allocate();
>
> If allocate doesn't return a pointer, but a boolean (or due to a refactor
> it changes) then the expression above would no longer do the right thing:
> assign 1 to var if either expression is true, which is not expected.
I don't know precisely what you mean by "inverted" logic
My bad, that's not true. I was thinking of &&.
, but I don't agree that it would necessarily confuse anyone who comes
across it - it would just be unfamiliar syntax, which they would look up.
It's true that the expression would compile regardless of whether
allocate() returns a bool or a pointer, which could cause a surprising bug
due to implicit pointer-to-bool conversion, but I don't feel like that's
very likely, or that it would be very likely that a refactor would cause a
problem there either. Specifically if your allocate() is changed from
returning a pointer to returning a bool during a refactor, its signature
would change in other ways that would cause a compile error there, i.e.
taking a reference to the pointer to allocate as an argument instead of
returning it.
That is true, my example was pretty bad.
Jefferson Carpenter
--
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/05da71c2-80cf-63dd-b2c6-
8cd342db5140%40gmail.com.
--
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/CALmDwq1SROgxcm8%3D7q2WaC%3DF4-cfkUycvEC6oCroEna-KFDZ8Q%40mail.gmail.com.
--089e08e5c2e9f08919056720183d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto"><div class=3D"gmail_extra" dir=3D"auto"><div class=3D"gma=
il_quote"><br><blockquote class=3D"quote" style=3D"margin:0 0 0 .8ex;border=
-left:1px #ccc solid;padding-left:1ex"><div class=3D"quoted-text">
> Also that inverted logic has a high chance of confusing anyone who com=
es<br>
> across it, and it some cases, it is not even clear which or operator y=
ou<br>
> choose.<br>
><br>
> var =3D var || allocate();<br>
><br>
> If allocate doesn't return a pointer, but a boolean (or due to a r=
efactor<br>
> it changes) then the expression above would no longer do the right thi=
ng:<br>
> assign 1 to var if either expression is true, which is not expected.<b=
r>
<br></div>
I don't know precisely what you mean by "inverted" logic</blo=
ckquote></div></div><div dir=3D"auto"><br></div><div dir=3D"auto">My bad, t=
hat's not true. I was thinking of &&.</div><div dir=3D"auto"><b=
r></div><div class=3D"gmail_extra" dir=3D"auto"><div class=3D"gmail_quote">=
<blockquote class=3D"quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc=
solid;padding-left:1ex">, but I don't agree that it would necessarily =
confuse anyone who comes across it - it would just be unfamiliar syntax, wh=
ich they would look up.=C2=A0 It's true that the expression would compi=
le regardless of whether allocate() returns a bool or a pointer, which coul=
d cause a surprising bug due to implicit pointer-to-bool conversion, but I =
don't feel like that's very likely, or that it would be very likely=
that a refactor would cause a problem there either.=C2=A0 Specifically if =
your allocate() is changed from returning a pointer to returning a bool dur=
ing a refactor, its signature would change in other ways that would cause a=
compile error there, i.e. taking a reference to the pointer to allocate as=
an argument instead of returning it.</blockquote></div></div><div dir=3D"a=
uto"><br></div><div dir=3D"auto">That is true, my example was pretty bad.</=
div><div dir=3D"auto"><br></div><div class=3D"gmail_extra" dir=3D"auto"><di=
v class=3D"gmail_quote"><blockquote class=3D"quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex"><div class=3D"quoted-text"=
>
<br>
Jefferson Carpenter<br>
<br>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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%2Bunsubscribe@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>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/05da71c2-80cf-63dd-b2c6-8cd342db5140%=
40gmail.com" rel=3D"noreferrer" target=3D"_blank">https://groups.google.com=
/a/is<wbr>ocpp.org/d/msgid/std-proposals<wbr>/05da71c2-80cf-63dd-b2c6-<wbr>=
8cd342db5140%40gmail.com</a>.<br>
</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" 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/CALmDwq1SROgxcm8%3D7q2WaC%3DF4-cfkUyc=
vEC6oCroEna-KFDZ8Q%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALmDwq1SROgx=
cm8%3D7q2WaC%3DF4-cfkUycvEC6oCroEna-KFDZ8Q%40mail.gmail.com</a>.<br />
--089e08e5c2e9f08919056720183d--
.
Author: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Date: Sun, 11 Mar 2018 19:06:37 +0100
Raw View
On Sun, Mar 11, 2018 at 9:38 AM, <jeffersoncarpenter2@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);
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/CANiq72kL7HQf2oS5THm8bxzurhzoc7uhNQq3S9%2BzvBce2yMZZQ%40mail.gmail.com.
.
Author: inkwizytoryankes@gmail.com
Date: Sun, 11 Mar 2018 17:28:34 -0700 (PDT)
Raw View
------=_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<<a href=3D"javascript:" target=3D"_blank" gdf-=
obfuscated-mailto=3D"HV-y43WUAAAJ" rel=3D"nofollow" onmousedown=3D"this.hre=
f=3D'javascript:';return true;" onclick=3D"this.href=3D'javascr=
ipt:';return true;">jefferson...@gmail.<wbr>com</a>> wrote:
<br>> Not sure how this will be received -- I haven't posted here mu=
ch. =C2=A0Just
<br>> thought it would be worth mentioning this idea.
<br>>
<br>> Suppose you have an API that you want to optionally allocate stora=
ge for
<br>> your user. =C2=A0They can either pass in a pointer to some existin=
g allocated
<br>> space, or pass in a null pointer and space is allocated for them.
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 if (x =3D=3D nullptr) {
<br>> =C2=A0 =C2=A0 x =3D new int;
<br>> =C2=A0 }
<br>> =C2=A0 // etc.
<br>> }
<br>>
<br>> Just figured it would be a decent idea if the || operator received=
a pointer
<br>> overload. =C2=A0Then you could write the more succinct
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x =3D x || new int;
<br>> }
<br>>
<br>> or even (ducks for cover)
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x ||=3D new int;
<br>> }
<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">=
([&](</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 <typename T, typename E>
<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 <typename T, expression E>
<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" 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--
.
Author: Richard Hodges <hodges.r@gmail.com>
Date: Mon, 12 Mar 2018 08:33:51 +0100
Raw View
--f403045fc23cdf5af00567322995
Content-Type: text/plain; charset="UTF-8"
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
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/
> isocpp.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/CALvx3hYz84MdBu_WsixPmWf8N%3D9FY4BiqOv%2BLon21nF2m_f0Ow%40mail.gmail.com.
--f403045fc23cdf5af00567322995
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I am a little surprised and dismayed at the eagerness to r=
esort to macros.<div><br></div><div>We can already safely overload operator=
s 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-crooked.com/a/bc751df610a220ce">http://coliru.sta=
cked-crooked.com/a/bc751df610a220ce</a><br></div><div><br></div><div><br></=
div><div><br></div><div><br></div></div><div class=3D"gmail_extra"><br><div=
class=3D"gmail_quote">On 12 March 2018 at 01:28, <span dir=3D"ltr"><<a=
href=3D"mailto:inkwizytoryankes@gmail.com" target=3D"_blank">inkwizytoryan=
kes@gmail.com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" st=
yle=3D"margin: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 O=
jeda wrote:<div><div class=3D"h5"><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<<a rel=3D"nofollow">jefferson..=
..@gmail.com</a>> wrote:
<br>> Not sure how this will be received -- I haven't posted here mu=
ch.=C2=A0 Just
<br>> thought it would be worth mentioning this idea.
<br>>
<br>> Suppose you have an API that you want to optionally allocate stora=
ge for
<br>> your user.=C2=A0 They can either pass in a pointer to some existin=
g allocated
<br>> space, or pass in a null pointer and space is allocated for them.
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 if (x =3D=3D nullptr) {
<br>> =C2=A0 =C2=A0 x =3D new int;
<br>> =C2=A0 }
<br>> =C2=A0 // etc.
<br>> }
<br>>
<br>> Just figured it would be a decent idea if the || operator received=
a pointer
<br>> overload.=C2=A0 Then you could write the more succinct
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x =3D x || new int;
<br>> }
<br>>
<br>> or even (ducks for cover)
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x ||=3D new int;
<br>> }
<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_867575031714103=
5639prettyprint"><code class=3D"m_8675750317141035639prettyprint"><div clas=
s=3D"m_8675750317141035639subprettyprint"><span style=3D"color:#000" class=
=3D"m_8675750317141035639styled-by-prettify"><br></span><span style=3D"colo=
r:#800" class=3D"m_8675750317141035639styled-by-prettify">#define</span><sp=
an style=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify"> =
OR_ELSE</span><span style=3D"color:#660" class=3D"m_8675750317141035639styl=
ed-by-prettify">(</span><span style=3D"color:#000" class=3D"m_8675750317141=
035639styled-by-prettify">v</span><span style=3D"color:#660" class=3D"m_867=
5750317141035639styled-by-prettify">,</span><span style=3D"color:#000" clas=
s=3D"m_8675750317141035639styled-by-prettify"> e</span><span style=3D"color=
:#660" class=3D"m_8675750317141035639styled-by-prettify">)</span><span styl=
e=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify"> </span>=
<span style=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify=
">([&](</span><span style=3D"color:#008" class=3D"m_8675750317141035639=
styled-by-prettify">auto</span><span style=3D"color:#000" class=3D"m_867575=
0317141035639styled-by-prettify"> MACRO_OR_ELSE_VALUE_X</span><span style=
=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify">){</span>=
<span style=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify=
"> </span><span style=3D"color:#008" class=3D"m_8675750317141035639styled-b=
y-prettify">return</span><span style=3D"color:#000" class=3D"m_867575031714=
1035639styled-by-prettify"> </span><span style=3D"color:#660" class=3D"m_86=
75750317141035639styled-by-prettify">(</span><span style=3D"color:#000" cla=
ss=3D"m_8675750317141035639styled-by-prettify">MACRO_OR_ELSE_VALUE_X </span=
><span style=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettif=
y">?</span><span style=3D"color:#000" class=3D"m_8675750317141035639styled-=
by-prettify"> MACRO_OR_ELSE_VALUE_X </span><span style=3D"color:#660" class=
=3D"m_8675750317141035639styled-by-prettify">:</span><span style=3D"color:#=
000" class=3D"m_8675750317141035639styled-by-prettify"> </span><span style=
=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify">(</span><=
span style=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify"=
>e</span><span style=3D"color:#660" class=3D"m_8675750317141035639styled-by=
-prettify">));</span><span style=3D"color:#000" class=3D"m_8675750317141035=
639styled-by-prettify"> </span><span style=3D"color:#660" class=3D"m_867575=
0317141035639styled-by-prettify">}(</span><span style=3D"color:#000" class=
=3D"m_8675750317141035639styled-by-prettify">v</span><span style=3D"color:#=
660" class=3D"m_8675750317141035639styled-by-prettify">))</span><span style=
=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify"><br><br><=
/span></div></code></div><br><br>This will allow evaluate `v` only once and=
can be inserted in other expressions:<br><br><div style=3D"background-colo=
r:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-=
width:1px" class=3D"m_8675750317141035639prettyprint"><code class=3D"m_8675=
750317141035639prettyprint"><div class=3D"m_8675750317141035639subprettypri=
nt"><span style=3D"color:#008" class=3D"m_8675750317141035639styled-by-pret=
tify">auto</span><span style=3D"color:#000" class=3D"m_8675750317141035639s=
tyled-by-prettify"> z </span><span style=3D"color:#660" class=3D"m_86757503=
17141035639styled-by-prettify">=3D</span><span style=3D"color:#000" class=
=3D"m_8675750317141035639styled-by-prettify"> h</span><span style=3D"color:=
#660" class=3D"m_8675750317141035639styled-by-prettify">()</span><span styl=
e=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify"> </span>=
<span style=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify=
">?</span><span style=3D"color:#000" class=3D"m_8675750317141035639styled-b=
y-prettify"> OR_ELSE</span><span style=3D"color:#660" class=3D"m_8675750317=
141035639styled-by-prettify">(</span><span style=3D"color:#000" class=3D"m_=
8675750317141035639styled-by-prettify">OR_ELSE</span><span style=3D"color:#=
660" class=3D"m_8675750317141035639styled-by-prettify">(</span><span style=
=3D"color:#000" class=3D"m_8675750317141035639styled-by-prettify">x</span><=
span style=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify"=
>,</span><span style=3D"color:#000" class=3D"m_8675750317141035639styled-by=
-prettify"> foo</span><span style=3D"color:#660" class=3D"m_867575031714103=
5639styled-by-prettify">()),</span><span style=3D"color:#000" class=3D"m_86=
75750317141035639styled-by-prettify"> bar</span><span style=3D"color:#660" =
class=3D"m_8675750317141035639styled-by-prettify">())</span><span style=3D"=
color:#000" class=3D"m_8675750317141035639styled-by-prettify"> </span><span=
style=3D"color:#660" class=3D"m_8675750317141035639styled-by-prettify">:</=
span><span style=3D"color:#000" class=3D"m_8675750317141035639styled-by-pre=
ttify"> zz</span><span style=3D"color:#660" class=3D"m_8675750317141035639s=
tyled-by-prettify">();</span><span style=3D"color:#000" class=3D"m_86757503=
17141035639styled-by-prettify"><br></span></div></code></div><br><br>=C2=A0=
</div><div><div class=3D"h5"><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">If you=
are really keen on having 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 <typename T, typename E>
<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 <typename T, expression E>
<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"h5">
<p></p>
-- <br>
You received this message because you are subscribed to the Google Groups &=
quot;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" target=3D"_=
blank">std-proposals+unsubscribe@<wbr>isocpp.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&utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/c7cd=
5d95-0031-4e50-<wbr>b7a1-9adcff695bd2%40isocpp.org</a><wbr>.<br>
</blockquote></div><br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/CALvx3hYz84MdBu_WsixPmWf8N%3D9FY4BiqO=
v%2BLon21nF2m_f0Ow%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CALvx3hYz84Md=
Bu_WsixPmWf8N%3D9FY4BiqOv%2BLon21nF2m_f0Ow%40mail.gmail.com</a>.<br />
--f403045fc23cdf5af00567322995--
.
Author: Larry Evans <cppljevans@suddenlink.net>
Date: Mon, 12 Mar 2018 05:32:52 -0500
Raw View
On 03/11/2018 03:38 AM, jeffersoncarpenter2@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;
> }
>
> would be nice.
>
There's precedent in another language, ruby. An example use is:
https://github.com/tardate/pdf-reader-turtletext/blob/master/lib/pdf/reader/positional_text_receiver.rb#L16
with explanation here:
http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html
--
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/p85ksl%24tij%241%40blaine.gmane.org.
.
Author: Richard Hodges <hodges.r@gmail.com>
Date: Mon, 12 Mar 2018 11:42:24 +0100
Raw View
--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"><<a href=
=3D"mailto:hodges.r@gmail.com" target=3D"_blank">hodges.r@gmail.com</a>>=
</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't take my posting "how-to" 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 "|=
=3D" case, but I don'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"><<a href=3D"m=
ailto:inkwizytoryankes@gmail.com" target=3D"_blank">inkwizytoryankes@gmail.=
com</a>></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<<a rel=3D"nofollow">j=
efferson...@gmail.com</a>> wrote:
<br>> Not sure how this will be received -- I haven't posted here mu=
ch.=C2=A0 Just
<br>> thought it would be worth mentioning this idea.
<br>>
<br>> Suppose you have an API that you want to optionally allocate stora=
ge for
<br>> your user.=C2=A0 They can either pass in a pointer to some existin=
g allocated
<br>> space, or pass in a null pointer and space is allocated for them.
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 if (x =3D=3D nullptr) {
<br>> =C2=A0 =C2=A0 x =3D new int;
<br>> =C2=A0 }
<br>> =C2=A0 // etc.
<br>> }
<br>>
<br>> Just figured it would be a decent idea if the || operator received=
a pointer
<br>> overload.=C2=A0 Then you could write the more succinct
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x =3D x || new int;
<br>> }
<br>>
<br>> or even (ducks for cover)
<br>>
<br>> void foo(int *x) {
<br>> =C2=A0 x ||=3D new int;
<br>> }
<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">([&](</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 <typename T, typename E>
<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 <typename T, expression E>
<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" 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&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" 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--
.
Author: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Date: Mon, 12 Mar 2018 12:06:43 +0100
Raw View
On Mon, Mar 12, 2018 at 8:33 AM, 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
Not sure what is your point. I already suggested the functor approach,
plus the restriction of the overloads as needed if he wanted to use
operator||.
As for the other approaches, I am unsure what do you find "magical"
about them. They are quite clear. :-)
By the way, embedding the code in the email is better for discussion
and archival purposes.
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/CANiq72mfBwiswcpwwBrdfdtYpPbDTe-ZNxHxaBhS5_vDUpagtA%40mail.gmail.com.
.
Author: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Date: Mon, 12 Mar 2018 12:18:27 +0100
Raw View
On Mon, Mar 12, 2018 at 1:28 AM, <inkwizytoryankes@gmail.com> wrote:
>
>
> On Sunday, March 11, 2018 at 7:06:59 PM UTC+1, Miguel Ojeda wrote:
>>
>> 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();
>
Indeed! The usual problems (and workarounds) with macros apply --
which is why it would have been great if C++ would have had something
similar to the "expression E" thingy I put above. :-)
Thanks for pointing it out!
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/CANiq72kDQVx6yaQQSBQCGvtZGtuYeVsSHNHFSanGXnDd82wa4A%40mail.gmail.com.
.
Author: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Date: Mon, 12 Mar 2018 12:41:56 +0100
Raw View
On Mon, Mar 12, 2018 at 11:42 AM, Richard Hodges <hodges.r@gmail.com> wrote:
>
> 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?
For me ||= would be the replace. But since we don't have ||=, I just
picked |=, which indeed looks fishy.
Now, what would be a nice experiment would be ||, &&, ||= and &&= with
short-circuit semantics, which could be implemented if we had the
"expression E" stuff:
template <typename T, expression E>
operator||=(T & t, E)
{
if (t)
return;
t = E;
}
Then:
x ||= new int;
would have the expected results.
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/CANiq72kNstxnyLTCOBfNorEiyudTijCAUHwH4DsoT14WqcNs4w%40mail.gmail.com.
.
Author: =?UTF-8?B?R2HFoXBlciBBxb5tYW4=?= <gasper.azman@gmail.com>
Date: Tue, 13 Mar 2018 11:26:23 +0000
Raw View
--f403045c5a2a7fd11405674988bb
Content-Type: text/plain; charset="UTF-8"
But my point was we *already have* an operator with the exact semantics of
the macro above - it's just a gnu extension. We could just propose that!
https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
x = x?:new int;
It's unambiguous, it's obvious it's a conditional, it's a one-liner, and
more importantly, it can be used for way more than just pointers. Oh, and
we have implementations and knowledge of how it behaves, and it's also not
a weird new form of || or |.
It also fails to compile if "x" and the expression if x is falsy don't have
a common type, which is *also* important.
So, I'd encourage the author of this proposal to instead propose the gnu
extension as an actual feature, instead of trying to fit a "don't evaluate
this" model as a special case for just certain types - making user-defined
types less like built-in types is an explicit no-no these days, and this
proposal is just trying to special-case pointers.
G
On Mon, Mar 12, 2018 at 11:41 AM, Miguel Ojeda <
miguel.ojeda.sandonis@gmail.com> wrote:
> On Mon, Mar 12, 2018 at 11:42 AM, Richard Hodges <hodges.r@gmail.com>
> wrote:
> >
> > 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?
>
> For me ||= would be the replace. But since we don't have ||=, I just
> picked |=, which indeed looks fishy.
>
> Now, what would be a nice experiment would be ||, &&, ||= and &&= with
> short-circuit semantics, which could be implemented if we had the
> "expression E" stuff:
>
> template <typename T, expression E>
> operator||=(T & t, E)
> {
> if (t)
> return;
> t = E;
> }
>
> Then:
>
> x ||= new int;
>
> would have the expected results.
>
> 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/CANiq72kNstxnyLTCOBfNorEiyudTi
> jCAUHwH4DsoT14WqcNs4w%40mail.gmail.com.
>
--
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/CAANG%3DkWjgCt-%2BpKOwWk6BgwNZ9ZECd9twWwq_FC1f3WPEiNw%3DA%40mail.gmail.com.
--f403045c5a2a7fd11405674988bb
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">But my point was we *already have* an operator with the ex=
act semantics of the macro above - it's just a gnu extension. We could =
just propose that!<div><br></div><div><a href=3D"https://gcc.gnu.org/online=
docs/gcc/Conditionals.html" target=3D"_blank">https://gcc.gnu.org/<wbr>onli=
nedocs/gcc/Conditionals.<wbr>html</a><br></div><div><br></div><div>x =3D x?=
:new int;</div><div><br></div><div>It's unambiguous, it's obvious i=
t's a conditional, it's a one-liner, and more importantly, it can b=
e used for way more than just pointers. Oh, and we have implementations and=
knowledge of how it behaves, and it's also not a weird new form of || =
or |.</div><div><br></div><div>It also fails to compile if "x" an=
d the expression if x is falsy don't have a common type, which is *also=
* important.</div><div><br></div><div>So, I'd encourage the author of t=
his proposal to instead propose the gnu extension as an actual feature, ins=
tead of trying to fit a "don't evaluate this" model as a spec=
ial case for just certain types - making user-defined types less like built=
-in types is an explicit no-no these days, and this proposal is just trying=
to special-case pointers.</div><div><br></div><div>G</div><div><br></div><=
/div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Mon, Mar =
12, 2018 at 11:41 AM, Miguel Ojeda <span dir=3D"ltr"><<a href=3D"mailto:=
miguel.ojeda.sandonis@gmail.com" target=3D"_blank">miguel.ojeda.sandonis@gm=
ail.com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D=
"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=
=3D"">On Mon, Mar 12, 2018 at 11:42 AM, Richard Hodges <<a href=3D"mailt=
o:hodges.r@gmail.com">hodges.r@gmail.com</a>> wrote:<br>
><br>
> Consider:<br>
><br>
> p |=3D new_thing();<br>
><br>
> In the above code, do we mean that we wish to replace p with a new thi=
ng if<br>
> empty, or does it mean that we wish to merge a new thing into the obje=
ct<br>
> referenced by p?<br>
<br>
</span>For me ||=3D would be the replace. But since we don't have ||=3D=
, I just<br>
picked |=3D, which indeed looks fishy.<br>
<br>
Now, what would be a nice experiment would be ||, &&, ||=3D and &am=
p;&=3D with<br>
short-circuit semantics, which could be implemented if we had the<br>
"expression E" stuff:<br>
<span class=3D""><br>
=C2=A0 =C2=A0 template <typename T, expression E><br>
</span>=C2=A0 =C2=A0 operator||=3D(T & t, E)<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (t)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t =3D E;<br>
=C2=A0 =C2=A0 }<br>
<br>
Then:<br>
<br>
=C2=A0 =C2=A0 x ||=3D new int;<br>
<br>
would have the expected results.<br>
<span class=3D""><br>
Cheers,<br>
Miguel<br>
<br>
--<br>
You received this message because you are subscribed to the Google Groups &=
quot;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%2Bunsubscribe@isocpp.org">std-propo=
sals+unsubscribe@<wbr>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>
</span>To view this discussion on the web visit <a href=3D"https://groups.g=
oogle.com/a/isocpp.org/d/msgid/std-proposals/CANiq72kNstxnyLTCOBfNorEiyudTi=
jCAUHwH4DsoT14WqcNs4w%40mail.gmail.com" rel=3D"noreferrer" target=3D"_blank=
">https://groups.google.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/<w=
br>CANiq72kNstxnyLTCOBfNorEiyudTi<wbr>jCAUHwH4DsoT14WqcNs4w%40mail.<wbr>gma=
il.com</a>.<br>
</blockquote></div><br></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/CAANG%3DkWjgCt-%2BpKOwWk6BgwNZ9ZECd9t=
wWwq_FC1f3WPEiNw%3DA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfoote=
r">https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAANG%3DkWj=
gCt-%2BpKOwWk6BgwNZ9ZECd9twWwq_FC1f3WPEiNw%3DA%40mail.gmail.com</a>.<br />
--f403045c5a2a7fd11405674988bb--
.
Author: Jakob Riedle <jakob.riedle@gmail.com>
Date: Fri, 16 Mar 2018 06:47:44 -0700 (PDT)
Raw View
------=_Part_1344_2017498275.1521208064482
Content-Type: multipart/alternative;
boundary="----=_Part_1345_1270386213.1521208064482"
------=_Part_1345_1270386213.1521208064482
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Am Dienstag, 13. M=C3=A4rz 2018 12:26:46 UTC+1 schrieb Ga=C5=A1per A=C5=BEm=
an:
>
> But my point was we *already have* an operator with the exact semantics o=
f=20
> the macro above - it's just a gnu extension. We could just propose that!
>
> https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
>
> x =3D x?:new int;
>
> It's unambiguous, it's obvious it's a conditional, it's a one-liner, and=
=20
> more importantly, it can be used for way more than just pointers. Oh, and=
=20
> we have implementations and knowledge of how it behaves, and it's also no=
t=20
> a weird new form of || or |.
>
> It also fails to compile if "x" and the expression if x is falsy don't=20
> have a common type, which is *also* important.
>
+1. I'm all about it!=20
--=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/462175d9-78de-4b2b-b440-3e317f3733a9%40isocpp.or=
g.
------=_Part_1345_1270386213.1521208064482
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><br><br>Am Dienstag, 13. M=C3=A4rz 2018 12:26:46 UTC+1 sch=
rieb Ga=C5=A1per A=C5=BEman:<blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><d=
iv dir=3D"ltr">But my point was we *already have* an operator with the exac=
t semantics of the macro above - it's just a gnu extension. We could ju=
st propose that!<div><br></div><div><a href=3D"https://gcc.gnu.org/onlinedo=
cs/gcc/Conditionals.html" target=3D"_blank" rel=3D"nofollow" onmousedown=3D=
"this.href=3D'https://www.google.com/url?q\x3dhttps%3A%2F%2Fgcc.gnu.org=
%2Fonlinedocs%2Fgcc%2FConditionals.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNH23eFTLoIJ2YmyrzhOWFGs_Ak7kg';return true;" onclick=3D"this.href=
=3D'https://www.google.com/url?q\x3dhttps%3A%2F%2Fgcc.gnu.org%2Fonlined=
ocs%2Fgcc%2FConditionals.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH23eF=
TLoIJ2YmyrzhOWFGs_Ak7kg';return true;">https://gcc.gnu.org/<wbr>onlined=
ocs/gcc/Conditionals.<wbr>html</a><br></div><div><br></div><div>x =3D x?:ne=
w int;</div><div><br></div><div>It's unambiguous, it's obvious it&#=
39;s a conditional, it's a one-liner, and more importantly, it can be u=
sed for way more than just pointers. Oh, and we have implementations and kn=
owledge of how it behaves, and it's also not a weird new form of || or =
|.</div><div><br></div><div>It also fails to compile if "x" and t=
he expression if x is falsy don't have a common type, which is *also* i=
mportant.</div></div></blockquote><div><br></div><div>+1. I'm all about=
it!=C2=A0</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/462175d9-78de-4b2b-b440-3e317f3733a9%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/462175d9-78de-4b2b-b440-3e317f3733a9=
%40isocpp.org</a>.<br />
------=_Part_1345_1270386213.1521208064482--
------=_Part_1344_2017498275.1521208064482--
.
Author: "T. C." <rs2740@gmail.com>
Date: Fri, 16 Mar 2018 15:12:40 -0700 (PDT)
Raw View
------=_Part_2769_1776788060.1521238360549
Content-Type: multipart/alternative;
boundary="----=_Part_2770_1313924794.1521238360550"
------=_Part_2770_1313924794.1521238360550
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
This was proposed in N4120 and rejected by EWG.
On Tuesday, March 13, 2018 at 7:26:46 AM UTC-4, Ga=C5=A1per A=C5=BEman wrot=
e:
>
> But my point was we *already have* an operator with the exact semantics o=
f=20
> the macro above - it's just a gnu extension. We could just propose that!
>
> https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
>
> x =3D x?:new int;
>
> It's unambiguous, it's obvious it's a conditional, it's a one-liner, and=
=20
> more importantly, it can be used for way more than just pointers. Oh, and=
=20
> we have implementations and knowledge of how it behaves, and it's also no=
t=20
> a weird new form of || or |.
>
> It also fails to compile if "x" and the expression if x is falsy don't=20
> have a common type, which is *also* important.
>
> So, I'd encourage the author of this proposal to instead propose the gnu=
=20
> extension as an actual feature, instead of trying to fit a "don't evaluat=
e=20
> this" model as a special case for just certain types - making user-define=
d=20
> types less like built-in types is an explicit no-no these days, and this=
=20
> proposal is just trying to special-case pointers.
>
> G
>
>
> On Mon, Mar 12, 2018 at 11:41 AM, Miguel Ojeda <miguel.oje...@gmail.com=
=20
> <javascript:>> wrote:
>
>> On Mon, Mar 12, 2018 at 11:42 AM, Richard Hodges <hodg...@gmail.com=20
>> <javascript:>> wrote:
>> >
>> > Consider:
>> >
>> > p |=3D new_thing();
>> >
>> > In the above code, do we mean that we wish to replace p with a new=20
>> thing if
>> > empty, or does it mean that we wish to merge a new thing into the obje=
ct
>> > referenced by p?
>>
>> For me ||=3D would be the replace. But since we don't have ||=3D, I just
>> picked |=3D, which indeed looks fishy.
>>
>> Now, what would be a nice experiment would be ||, &&, ||=3D and &&=3D wi=
th
>> short-circuit semantics, which could be implemented if we had the
>> "expression E" stuff:
>>
>> template <typename T, expression E>
>> operator||=3D(T & t, E)
>> {
>> if (t)
>> return;
>> t =3D E;
>> }
>>
>> Then:
>>
>> x ||=3D new int;
>>
>> would have the expected results.
>>
>> Cheers,
>> Miguel
>>
>> --
>> You received this message because you are subscribed to the Google Group=
s=20
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n=20
>> email to std-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> To view this discussion on the web visit=20
>> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CANiq72kNst=
xnyLTCOBfNorEiyudTijCAUHwH4DsoT14WqcNs4w%40mail.gmail.com
>> .
>>
>
>
--=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/8751b79c-2f93-4916-bd0e-51b8e3eb910f%40isocpp.or=
g.
------=_Part_2770_1313924794.1521238360550
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This was proposed in=C2=A0N4120 and rejected by EWG.<br><b=
r>On Tuesday, March 13, 2018 at 7:26:46 AM UTC-4, Ga=C5=A1per A=C5=BEman wr=
ote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex=
;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">But my po=
int was we *already have* an operator with the exact semantics of the macro=
above - it's just a gnu extension. We could just propose that!<div><br=
></div><div><a href=3D"https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html=
" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D'https:=
//www.google.com/url?q\x3dhttps%3A%2F%2Fgcc.gnu.org%2Fonlinedocs%2Fgcc%2FCo=
nditionals.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH23eFTLoIJ2YmyrzhOW=
FGs_Ak7kg';return true;" onclick=3D"this.href=3D'https://www.google=
..com/url?q\x3dhttps%3A%2F%2Fgcc.gnu.org%2Fonlinedocs%2Fgcc%2FConditionals.h=
tml\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH23eFTLoIJ2YmyrzhOWFGs_Ak7kg=
9;;return true;">https://gcc.gnu.org/<wbr>onlinedocs/gcc/Conditionals.<wbr>=
html</a><br></div><div><br></div><div>x =3D x?:new int;</div><div><br></div=
><div>It's unambiguous, it's obvious it's a conditional, it'=
;s a one-liner, and more importantly, it can be used for way more than just=
pointers. Oh, and we have implementations and knowledge of how it behaves,=
and it's also not a weird new form of || or |.</div><div><br></div><di=
v>It also fails to compile if "x" and the expression if x is fals=
y don't have a common type, which is *also* important.</div><div><br></=
div><div>So, I'd encourage the author of this proposal to instead propo=
se the gnu extension as an actual feature, instead of trying to fit a "=
;don't evaluate this" model as a special case for just certain typ=
es - making user-defined types less like built-in types is an explicit no-n=
o these days, and this proposal is just trying to special-case pointers.</d=
iv><div><br></div><div>G</div><div><br></div></div><div><br><div class=3D"g=
mail_quote">On Mon, Mar 12, 2018 at 11:41 AM, Miguel Ojeda <span dir=3D"ltr=
"><<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"Q8=
e4LcgbAQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:=
9;;return true;" onclick=3D"this.href=3D'javascript:';return true;"=
>miguel.oje...@gmail.<wbr>com</a>></span> wrote:<br><blockquote class=3D=
"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding=
-left:1ex"><span>On Mon, Mar 12, 2018 at 11:42 AM, Richard Hodges <<a hr=
ef=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"Q8e4LcgbAQAJ"=
rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:';return t=
rue;" onclick=3D"this.href=3D'javascript:';return true;">hodg...@gm=
ail.com</a>> wrote:<br>
><br>
> Consider:<br>
><br>
> p |=3D new_thing();<br>
><br>
> In the above code, do we mean that we wish to replace p with a new thi=
ng if<br>
> empty, or does it mean that we wish to merge a new thing into the obje=
ct<br>
> referenced by p?<br>
<br>
</span>For me ||=3D would be the replace. But since we don't have ||=3D=
, I just<br>
picked |=3D, which indeed looks fishy.<br>
<br>
Now, what would be a nice experiment would be ||, &&, ||=3D and &am=
p;&=3D with<br>
short-circuit semantics, which could be implemented if we had the<br>
"expression E" stuff:<br>
<span><br>
=C2=A0 =C2=A0 template <typename T, expression E><br>
</span>=C2=A0 =C2=A0 operator||=3D(T & t, E)<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (t)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 t =3D E;<br>
=C2=A0 =C2=A0 }<br>
<br>
Then:<br>
<br>
=C2=A0 =C2=A0 x ||=3D new int;<br>
<br>
would have the expected results.<br>
<span><br>
Cheers,<br>
Miguel<br>
<br>
--<br>
You received this message because you are subscribed to the Google Groups &=
quot;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"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
Q8e4LcgbAQAJ" rel=3D"nofollow" onmousedown=3D"this.href=3D'javascript:&=
#39;;return true;" onclick=3D"this.href=3D'javascript:';return true=
;">std-proposal...@<wbr>isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"javascript:" target=3D"_bla=
nk" gdf-obfuscated-mailto=3D"Q8e4LcgbAQAJ" rel=3D"nofollow" onmousedown=3D"=
this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'=
;javascript:';return true;">std-pr...@isocpp.org</a>.<br>
</span>To view this discussion on the web visit <a href=3D"https://groups.g=
oogle.com/a/isocpp.org/d/msgid/std-proposals/CANiq72kNstxnyLTCOBfNorEiyudTi=
jCAUHwH4DsoT14WqcNs4w%40mail.gmail.com" rel=3D"nofollow" target=3D"_blank" =
onmousedown=3D"this.href=3D'https://groups.google.com/a/isocpp.org/d/ms=
gid/std-proposals/CANiq72kNstxnyLTCOBfNorEiyudTijCAUHwH4DsoT14WqcNs4w%40mai=
l.gmail.com';return true;" onclick=3D"this.href=3D'https://groups.g=
oogle.com/a/isocpp.org/d/msgid/std-proposals/CANiq72kNstxnyLTCOBfNorEiyudTi=
jCAUHwH4DsoT14WqcNs4w%40mail.gmail.com';return true;">https://groups.go=
ogle.com/a/<wbr>isocpp.org/d/msgid/std-<wbr>proposals/<wbr>CANiq72kNstxnyLT=
COBfNorEiyudTi<wbr>jCAUHwH4DsoT14WqcNs4w%40mail.<wbr>gmail.com</a>.<br>
</blockquote></div><br></div>
</blockquote></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;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/8751b79c-2f93-4916-bd0e-51b8e3eb910f%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8751b79c-2f93-4916-bd0e-51b8e3eb910f=
%40isocpp.org</a>.<br />
------=_Part_2770_1313924794.1521238360550--
------=_Part_2769_1776788060.1521238360549--
.
Author: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Date: Sat, 17 Mar 2018 05:38:52 +0100
Raw View
On Tue, Mar 13, 2018 at 12:26 PM, Ga=C5=A1per A=C5=BEman <gasper.azman@gmai=
l.com> wrote:
> But my point was we *already have* an operator with the exact semantics o=
f
> the macro above - it's just a gnu extension. We could just propose that!
>
> https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html
>
> x =3D x?:new int;
>
> It's unambiguous, it's obvious it's a conditional, it's a one-liner, and
The question is: what do you gain by introducing it? Clarity? I am not
convinced it is simpler than the alternatives. Expressiveness? You can
already do the same in roughly the same amount of code (and also "in
one line" as well).
Moreover, for complex types, this:
if (x) x =3D y;
can potentially be much better, because the assignment is only done in one =
case.
> more importantly, it can be used for way more than just pointers. Oh, and=
we
> have implementations and knowledge of how it behaves, and it's also not a
> weird new form of || or |.
What "new forms"? They are just overloads, and adding new operators is
about as confusing as operator overloading. In both cases, you end up
having to memorize yet another idiom that people will use in some --
but not all -- projects.
In addition, ||, | and |=3D can currently be used for anything as well,
not just pointers. This is not the same as saying that they *should*,
but it is not an advantage of ?: either.
Cheers,
Miguel
--=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/CANiq72khnVdSaUapRcgb3fHZotYntSs99uC3a9fBc%2BKJW=
OO0HA%40mail.gmail.com.
.
Author: Matthew Woehlke <mwoehlke.floss@gmail.com>
Date: Thu, 26 Jul 2018 16:15:10 -0400
Raw View
On 2018-03-11 04:38, jeffersoncarpenter2@gmail.com wrote:
> 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;
> }
Apologies for the thread necro, but I'm surprised no one mentioned:
x or (x = new int); // s/or/||/ if you prefer
--
Matthew
--
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/6e223f06-c585-aadd-8616-35f51439fe81%40gmail.com.
.