Topic: Uniform initialization in ternary conditional expression
Author: zwvista@msn.com
Date: Fri, 11 Jul 2014 08:50:07 -0700 (PDT)
Raw View
------=_Part_65_33332675.1405093807936
Content-Type: text/plain; charset=UTF-8
It's annoying that I cannot use brace initialization in a ternary
conditional expression.
As all know, we can write
int i = some_condition ? 3 : 4;
instead of
int i;
if(some_condition)
i = 3;
else
i = 4;
But I cannot write
vector<int> v = some_condition ? {3} : {4}; // not valid C++
instead of
vector<int> v;
if(some_condition)
v = {3};
else
v = {4};
I can only write
auto v = some_condition ? vector<int>{3} : vector<int>{4};
but the type vector<int> must be repeated here.
Can we make the following code valid?
vector<int> v = some_condition ? {3} : {4};
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_65_33332675.1405093807936
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">It's annoying that I cannot use brace initialization in a =
ternary conditional expression.<div>As all know, we can write </div><d=
iv><span style=3D"font-size: 13px;">int i =3D some_condition ? 3 : 4;</span=
><br></div><div><span style=3D"font-size: 13px;">instead of</span><br></div=
><div><span style=3D"font-size: 13px;">int i;</span><br></div><div>if(some_=
condition)</div><div> i =3D 3;</div><div>else</div><div> =
i =3D 4;</div><div><br></div><div>But I cannot write</div><div><span =
style=3D"font-size: 13px;">vector<int> v =3D some_condition ? {3} : {=
4}; // not valid C++</span><br></div><div><span style=3D"font-size: 13px;">=
instead of</span><br></div><div><span style=3D"font-size: 13px;">vector<=
int> v;</span><br></div><div>if(some_condition)</div><div> v=
=3D {3};</div><div>else</div><div> v =3D {4};</div><div><br></=
div><div>I can only write </div><div>auto v =3D some_condition ? vecto=
r<int>{3} : vector<int>{4};</div><div>but the type vector<in=
t> must be repeated here.</div><div><br></div><div>Can we make the follo=
wing code valid?</div><div><span style=3D"font-size: 13px;">vector<int&g=
t; v =3D some_condition ? {3} : {4};</span><br></div></div>
<p></p>
-- <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+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_65_33332675.1405093807936--
.
Author: David Krauss <potswa@gmail.com>
Date: Sat, 12 Jul 2014 09:19:32 +0800
Raw View
--Apple-Mail=_4AA4B75C-8791-4109-BAAD-B934703948F4
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
On 2014-07-11, at 11:50 PM, zwvista@msn.com wrote:
> It's annoying that I cannot use brace initialization in a ternary conditi=
onal expression.
I just replied regarding this very issue on StackOverflow. Interesting that=
even comments there amount to publicity. Here is what I said:
> That may not be a parsing issue, but a type deduction issue. Both alterna=
tives of the ternary operator need to agree on a common type, and something=
also has to determine the result value category. The braces obscure the me=
aning of the program. It's better in ?: to be explicit and put the type-nam=
e before the {, and this doesn't sacrifice any expressive power.
--=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.
--Apple-Mail=_4AA4B75C-8791-4109-BAAD-B934703948F4
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1
<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;07–11, at 11:50 PM, <a href=3D"mailto:zwvista@msn.com">zwvista@=
msn.com</a> wrote:</div><br class=3D"Apple-interchange-newline"><blockquote=
type=3D"cite"><div dir=3D"ltr">It's annoying that I cannot use brace initi=
alization in a ternary conditional expression.</div></blockquote><div><br><=
/div><div>I just <a href=3D"http://stackoverflow.com/questions/1142044=
8/initializer-lists-and-rhs-of-operators/11445905#comment38319702_11445905"=
>replied</a> regarding this very issue on StackOverflow. Interesting t=
hat even comments there amount to publicity. Here is what I said:</div><div=
><br></div><div></div><blockquote type=3D"cite"><div><span class=3D"comment=
s">That may not be a parsing issue, but a type deduction issue. Both altern=
atives of the ternary operator need to agree on a common type, and somethin=
g also has to determine the result value category. The braces obscure the m=
eaning of the program. It's better in <code>?:</code> to be expli=
cit and put the type-name before the <code>{</code>, and this doesn't =
sacrifice any expressive power.</span></div></blockquote><div><br></div></d=
iv></body></html>
<p></p>
-- <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+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
--Apple-Mail=_4AA4B75C-8791-4109-BAAD-B934703948F4--
.