Topic: comparison operators for std::initializer_list


Author: prithviy@umich.edu
Date: Fri, 25 Jan 2019 06:00:37 -0800 (PST)
Raw View
------=_Part_714_1783437998.1548424837959
Content-Type: multipart/alternative;
 boundary="----=_Part_715_1232152741.1548424837960"

------=_Part_715_1232152741.1548424837960
Content-Type: text/plain; charset="UTF-8"




std::array, std::tuple and std::pair all have operator< defined to compare
instances of similar template parameters (element-wise comparison).


Would it be a good idea to implement operator< (and >,==,!=) for
std::initializer_list<T>?


Implementing such functions will be straightforward and will allow people
to use something more natural in cases where they want to compare two lists
as opposed to having to fallback to algorithms with iterators.


These functions can be marked constexpr as well.


Currently, such code will not compile:


auto A = {1,2};
auto B = {3,4};

if (A < B)
{
    cout << "YES" << endl;

}
else
{
    cout << "NO" << endl;
}


--
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/24be1c08-b474-4e08-8b3f-0d6cbb4d9a8e%40isocpp.org.

------=_Part_715_1232152741.1548424837960
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">




<style type=3D"text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color=
: #454545; min-height: 14.0px}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color=
: #454545}
</style>


<p class=3D"p1"><br></p>
<p class=3D"p2"><font face=3D"courier new, monospace">std::array, std::tupl=
e</font> and <font face=3D"courier new, monospace">std::pair</font> all hav=
e <font face=3D"courier new, monospace">operator&lt; </font>defined to comp=
are instances of similar template parameters (element-wise comparison).<spa=
n class=3D"Apple-converted-space">=C2=A0</span></p>
<p class=3D"p1"><br></p>
<p class=3D"p2">Would it be a good idea to implement <font face=3D"courier =
new, monospace">operator&lt;</font> (and &gt;,=3D=3D,!=3D) for <font face=
=3D"courier new, monospace">std::initializer_list&lt;T&gt;</font>?<span cla=
ss=3D"Apple-converted-space">=C2=A0</span></p>
<p class=3D"p1"><br></p>
<p class=3D"p2">Implementing such functions will be straightforward and wil=
l allow people to use something more natural in cases where they want to co=
mpare two lists as opposed to having to fallback to algorithms with iterato=
rs.</p>
<p class=3D"p1"><br></p>
<p class=3D"p2">These functions can be marked <font face=3D"courier new, mo=
nospace">constexpr</font> as well.</p>
<p class=3D"p1"><br></p>
<p class=3D"p2">Currently, such code will not compile:</p>
<p class=3D"p1"><br></p>
<div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); b=
order-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; ov=
erflow-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpret=
typrint"><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> A </span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #066;" =
class=3D"styled-by-prettify">1</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">,</span><span style=3D"color: #066;" class=3D"styled-by=
-prettify">2</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> B </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">{</span><span style=3D"color: #066;" clas=
s=3D"styled-by-prettify">3</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">,</span><span style=3D"color: #066;" class=3D"styled-by-pre=
ttify">4</span><span style=3D"color: #660;" class=3D"styled-by-prettify">};=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br><br><=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">if</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify">A </span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> B</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> <br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <br>=
=C2=A0 =C2=A0 cout </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">&lt;&lt;</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify">&q=
uot;YES&quot;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;&l=
t;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> endl</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span=
 style=3D"color: #000;" class=3D"styled-by-prettify"> =C2=A0<br><br></span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> <br></span><span style=3D=
"color: #008;" class=3D"styled-by-prettify">else</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> <br></span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> <br>=C2=A0 =C2=A0 cout </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;" cl=
ass=3D"styled-by-prettify">&quot;NO&quot;</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&lt;&lt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> endl</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> <br></span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">}</span></div></code></div><p class=3D"p2"><br></p></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/24be1c08-b474-4e08-8b3f-0d6cbb4d9a8e%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/24be1c08-b474-4e08-8b3f-0d6cbb4d9a8e=
%40isocpp.org</a>.<br />

------=_Part_715_1232152741.1548424837960--

------=_Part_714_1783437998.1548424837959--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Fri, 25 Jan 2019 07:02:21 -0800 (PST)
Raw View
------=_Part_816_1305729088.1548428541833
Content-Type: multipart/alternative;
 boundary="----=_Part_817_748329377.1548428541833"

------=_Part_817_748329377.1548428541833
Content-Type: text/plain; charset="UTF-8"

An `initializer_list`, as the name suggests, is a list meant to be used to
initialize an object. That is why it exists: to help you initialize an
object from a homogeneous sequence of values.

If you're doing comparisons between such lists, then I submit that you're
using the object incorrectly, that it's being used for a purpose outside of
its intended use case. Thanks to CTAD, we can do `std::array a = {1, 2};`,
so there's really no reason to use an `auto`-deduced `initializer_list`
instead of a proper type.

--
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/1828d3a1-5211-4b53-9521-e926f7fa0fb7%40isocpp.org.

------=_Part_817_748329377.1548428541833
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>An `initializer_list`, as the name suggests, is a lis=
t meant to be used to initialize an object. That is why it exists: to help =
you initialize an object from a homogeneous sequence of values.<br></div><d=
iv><br></div><div>If you&#39;re doing comparisons between such lists, then =
I submit that you&#39;re using the object incorrectly, that it&#39;s being =
used for a purpose outside of its intended use case. Thanks to CTAD, we can=
 do `std::array a =3D {1, 2};`, so there&#39;s really no reason to use an `=
auto`-deduced `initializer_list` instead of a proper type.<br></div></div>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/1828d3a1-5211-4b53-9521-e926f7fa0fb7%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1828d3a1-5211-4b53-9521-e926f7fa0fb7=
%40isocpp.org</a>.<br />

------=_Part_817_748329377.1548428541833--

------=_Part_816_1305729088.1548428541833--

.