Topic: Overloading std::distance for std::pair


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Wed, 21 Oct 2015 11:32:00 -0700 (PDT)
Raw View
------=_Part_5418_1169875921.1445452320501
Content-Type: multipart/alternative;
 boundary="----=_Part_5419_349116580.1445452320501"

------=_Part_5419_349116580.1445452320501
Content-Type: text/plain; charset=UTF-8

I would like to suggest a proposal relative to overloading the standard
function std::distance for objects of type std::pair.

Some standard algorithms like for example std::equal_range or user-defined
algorithms return a pair of iterators.

Consider for example a task that you have two pairs of ranges returned by
std::equal_range and want to know what range is greater

Let's assume that the first range is named like first_range and the second
range is named like second_range. In this case an if statement could look
like

if ( std::distance( first_range.first, first_range.second ) <
std::distance( second_range.first, second_range.second ) )
{
    //...
}

As you see the statement is too long.:)

It would be better to write

if( std:;distance( first_range ) < std::distance( second_range ) )
{
    //...
}

The function can be defined the following way

template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type
distance( pair<InputIterator, InputIterator> iterator_pair )
{
    return distance( iterator_pair.first, iterator_pair.second );
}

--

---
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_5419_349116580.1445452320501
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I would like to suggest a proposal relative to overlo=
ading the standard function std::distance for objects of type std::pair.</d=
iv><div><br></div><div>Some standard algorithms like for example std::equal=
_range or user-defined algorithms return a pair of iterators.</div><div><br=
></div><div>Consider for example=C2=A0a task that you have two pairs of ran=
ges returned by std::equal_range and want to know what range is greater</di=
v><div><br></div><div>Let&#39;s assume that the first range is named like f=
irst_range and the second range is named like second_range. In this case an=
 if statement could look like</div><div><br></div><div>if ( std::distance( =
first_range.first, first_range.second ) &lt; std::distance( second_range.fi=
rst, second_range.second ) )</div><div>{</div><div>=C2=A0=C2=A0=C2=A0 //...=
</div><div>}</div><div><br></div><div>As you see the statement is too long.=
:)</div><div><br></div><div>It would be better to write</div><div><br></div=
><div>if( std:;distance( first_range ) &lt; std::distance( second_range ) )=
</div><div>{</div><div>=C2=A0=C2=A0=C2=A0 //...</div><div>}</div><div><br><=
/div><div>The function can be defined the following way</div><div><br></div=
><div>template &lt;class InputIterator&gt;<br>typename iterator_traits&lt;I=
nputIterator&gt;::difference_type<br>distance( pair&lt;InputIterator, Input=
Iterator&gt; iterator_pair )<br>{<br>=C2=A0=C2=A0=C2=A0 return distance( it=
erator_pair.first, iterator_pair.second );<br>}<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&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 />
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_5419_349116580.1445452320501--
------=_Part_5418_1169875921.1445452320501--

.


Author: Sam Kellett <samkellett@gmail.com>
Date: Wed, 21 Oct 2015 22:51:58 +0100
Raw View
--047d7b3a85d23ca0990522a463dd
Content-Type: text/plain; charset=UTF-8

On 21 October 2015 at 19:32, Vlad from Moscow <vlad.moscow@mail.ru> wrote:

I would like to suggest a proposal relative to overloading the standard
> function std::distance for objects of type std::pair.
>
> Some standard algorithms like for example std::equal_range or user-defined
> algorithms return a pair of iterators.
>
> Consider for example a task that you have two pairs of ranges returned by
> std::equal_range and want to know what range is greater
>
> Let's assume that the first range is named like first_range and the second
> range is named like second_range. In this case an if statement could look
> like
>
> if ( std::distance( first_range.first, first_range.second ) <
> std::distance( second_range.first, second_range.second ) )
> {
>     //...
> }
>
> As you see the statement is too long.:)
>
> It would be better to write
>
> if( std:;distance( first_range ) < std::distance( second_range ) )
> {
>     //...
> }
>
> The function can be defined the following way
>
> template <class InputIterator>
> typename iterator_traits<InputIterator>::difference_type
> distance( pair<InputIterator, InputIterator> iterator_pair )
> {
>     return distance( iterator_pair.first, iterator_pair.second );
> }
>

i would hazard a guess that this will be susceptible to the same opposition
that an earlier proposal of yours faced:
https://groups.google.com/a/isocpp.org/d/msg/std-proposals/z_BtdcvO1NA/p8k3VYGme9AJ

namely a pair isn't a range. and true ranges are coming.

--

---
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/.

--047d7b3a85d23ca0990522a463dd
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On 21 October 2015 at 19:32, Vlad from Moscow <span dir=3D=
"ltr">&lt;<a href=3D"mailto:vlad.moscow@mail.ru" target=3D"_blank">vlad.mos=
cow@mail.ru</a>&gt;</span> wrote:<br><div class=3D"gmail_extra"><div class=
=3D"gmail_quote"><br><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div=
 dir=3D"ltr"><div>I would like to suggest a proposal relative to overloadin=
g the standard function std::distance for objects of type std::pair.</div><=
div><br></div><div>Some standard algorithms like for example std::equal_ran=
ge or user-defined algorithms return a pair of iterators.</div><div><br></d=
iv><div>Consider for example=C2=A0a task that you have two pairs of ranges =
returned by std::equal_range and want to know what range is greater</div><d=
iv><br></div><div>Let&#39;s assume that the first range is named like first=
_range and the second range is named like second_range. In this case an if =
statement could look like</div><div><br></div><div>if ( std::distance( firs=
t_range.first, first_range.second ) &lt; std::distance( second_range.first,=
 second_range.second ) )</div><div>{</div><div>=C2=A0=C2=A0=C2=A0 //...</di=
v><div>}</div><div><br></div><div>As you see the statement is too long.:)</=
div><div><br></div><div>It would be better to write</div><div><br></div><di=
v>if( std:;distance( first_range ) &lt; std::distance( second_range ) )</di=
v><div>{</div><div>=C2=A0=C2=A0=C2=A0 //...</div><div>}</div><div><br></div=
><div>The function can be defined the following way</div><div><br></div><di=
v>template &lt;class InputIterator&gt;<br>typename iterator_traits&lt;Input=
Iterator&gt;::difference_type<br>distance( pair&lt;InputIterator, InputIter=
ator&gt; iterator_pair )<br>{<br>=C2=A0=C2=A0=C2=A0 return distance( iterat=
or_pair.first, iterator_pair.second );<br>}<span class=3D""><font color=3D"=
#888888"><br></font></span></div></div><span class=3D""></span></blockquote=
><div><br></div><div>i would hazard a guess that this will be susceptible t=
o the same opposition that an earlier proposal of yours faced: <a href=3D"h=
ttps://groups.google.com/a/isocpp.org/d/msg/std-proposals/z_BtdcvO1NA/p8k3V=
YGme9AJ">https://groups.google.com/a/isocpp.org/d/msg/std-proposals/z_Btdcv=
O1NA/p8k3VYGme9AJ</a><br><br></div><div>namely a pair isn&#39;t a range. an=
d true ranges are coming. <br></div></div></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&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 />
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 />

--047d7b3a85d23ca0990522a463dd--

.