Topic: move_if proposal
Author: Adrian Hawryluk <adrian.hawryluk@gmail.com>
Date: Mon, 1 Apr 2019 16:51:43 -0400
Raw View
--000000000000cef6ea05857e3030
Content-Type: text/plain; charset="UTF-8"
I was looking around and found a few mentions about using copy_if() with
move iterators. After thinking about this, I realised that this could
leave valid but unspecified objects scattered around in the container,
which could cause problems. To stop this from happening, can there not be
a std::move_if() algorithm declared and *maybe* have std::copy_if() deleted
for move iterators to prevent such problems from occurring (not sure if
there could be valid use cases for such behaviour)? My proposal would be a
function like the following:
template <typename Iter, typename Iter_dest, typename Pred>
Iter move_if(Iter begin, Iter end, Iter_dest& dest, Pred pred)
{
Iter it_removed = end;
while (begin != end) {
if (pred(*begin)) {
*dest = std::move(*begin);
it_removed = begin++;
while (begin != end) {
if (pred(*begin)) {
*dest = std::move(*begin);
}
else {
*(it_removed++) = std::move(*begin);
}
++begin;
}
break;
}
++begin;
}
return it_removed;
}
template <typename Iter, typename Iter_dest, typename Pred>
Iter move_if(Iter begin, Iter end, Iter_dest&& dest, Pred pred)
{
return move_if(begin, end, dest, pred);
}
Where like std::remove_if(), it would return the position of where the
valid but unspecified objects begin, but unlike std::copy_if(), it would
have the dest parameter as a reference to an iterator, so that it can be
tracked if wanted after the call. If dest is a rvalue, then it would then
it's just use it as an lvalue as the value doesn't matter after the call
anyway. It's a simple addition, but one that could prevent nubes from
problems, and simplify more advanced users lives.
--
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/CAP_kE8VYJGmVFoO0V4cDZPXjScEc97tYfqVh92D5C9zFYS9%3DOw%40mail.gmail.com.
--000000000000cef6ea05857e3030
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr"><div dir=3D"ltr">I was l=
ooking around and found a few mentions about using <font face=3D"monospace,=
monospace">copy_if()</font>=C2=A0with move iterators.=C2=A0 After thinking=
about this, I realised that this could leave valid but unspecified objects=
scattered around in the container, which could cause problems.=C2=A0 To st=
op this from happening, can there not be a <font face=3D"monospace, monospa=
ce">std::move_if()</font> algorithm declared and <i>maybe</i> have <font fa=
ce=3D"monospace, monospace">std::copy_if()</font> deleted for move iterator=
s to prevent such problems from occurring (not sure if there could be valid=
use cases for such behaviour)?=C2=A0 My proposal would be a function like =
the following:<br><div><br></div><div><div><font face=3D"monospace, monospa=
ce">template <typename Iter, typename Iter_dest, typename Pred></font=
></div><div><font face=3D"monospace, monospace">Iter move_if(Iter begin, It=
er end, Iter_dest& dest, Pred pred)</font></div><div><font face=3D"mono=
space, monospace">{</font></div><div><font face=3D"monospace, monospace">=
=C2=A0 Iter it_removed =3D end;</font></div><div><font face=3D"monospace, m=
onospace">=C2=A0 while (begin !=3D end) {</font></div><div><font face=3D"mo=
nospace, monospace">=C2=A0 =C2=A0 if (pred(*begin)) {</font></div><div><fon=
t face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 *dest =3D std::move(*b=
egin);</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =
=C2=A0 it_removed =3D begin++;</font></div><div><font face=3D"monospace, mo=
nospace">=C2=A0 =C2=A0 =C2=A0 while (begin !=3D end) {</font></div><div><fo=
nt face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (pred(*begi=
n)) {</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 *dest =3D std::move(*begin);</font></div><div><font fa=
ce=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</font></div><div>=
<font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 else {</fon=
t></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 *(it_removed++) =3D std::move(*begin);</font></div><div><font fa=
ce=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</font></div><div>=
<font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 ++begin;</f=
ont></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 }</=
font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 br=
eak;=C2=A0=C2=A0</font></div><div><font face=3D"monospace, monospace">=C2=
=A0 =C2=A0 }</font></div><div><font face=3D"monospace, monospace">=C2=A0 =
=C2=A0 ++begin;</font></div><div><font face=3D"monospace, monospace">=C2=A0=
}</font></div><div><font face=3D"monospace, monospace">=C2=A0 return it_re=
moved;</font></div><div><font face=3D"monospace, monospace">}</font></div><=
div><font face=3D"monospace, monospace"><br></font></div><div><font face=3D=
"monospace, monospace">template <typename Iter, typename Iter_dest, type=
name Pred></font></div><div><font face=3D"monospace, monospace">Iter mov=
e_if(Iter begin, Iter end, Iter_dest&& dest, Pred pred)</font></div=
><div><font face=3D"monospace, monospace">{</font></div><div><font face=3D"=
monospace, monospace">=C2=A0 return move_if(begin, end, dest, pred);</font>=
</div><div><font face=3D"monospace, monospace">}</font></div><div><br></div=
><div>Where like <font face=3D"monospace, monospace">std::remove_if()</font=
>, it would return the position of where the valid but unspecified objects =
begin, but unlike <font face=3D"monospace, monospace">std::copy_if()</font>=
, it would have the <font face=3D"monospace, monospace">dest</font> paramet=
er as a reference to an iterator, so that it can be tracked if wanted after=
the call.=C2=A0 If <font face=3D"monospace, monospace">dest</font> is a rv=
alue, then it would then it's just use it as an lvalue as the value doe=
sn't matter after the call anyway.=C2=A0 It's a simple addition, bu=
t one that could prevent nubes from problems, and simplify more advanced us=
ers lives.<br></div></div><div><br></div></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/CAP_kE8VYJGmVFoO0V4cDZPXjScEc97tYfqVh=
92D5C9zFYS9%3DOw%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter">h=
ttps://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAP_kE8VYJGmVFo=
O0V4cDZPXjScEc97tYfqVh92D5C9zFYS9%3DOw%40mail.gmail.com</a>.<br />
--000000000000cef6ea05857e3030--
.
Author: Adrian H <adrian.hawryluk@gmail.com>
Date: Tue, 2 Apr 2019 04:56:36 -0700 (PDT)
Raw View
------=_Part_295_664121668.1554206196831
Content-Type: multipart/alternative;
boundary="----=_Part_296_521857661.1554206196831"
------=_Part_296_521857661.1554206196831
Content-Type: text/plain; charset="UTF-8"
I was looking around and found a few mentions about using copy_if() with
move iterators. After thinking about this, I realised that this could
leave valid but unspecified objects scattered around in the container,
which could cause problems. To stop this from happening, can there not be
a std::move_if() algorithm declared and *maybe* have std::copy_if() deleted
for move iterators to prevent such problems from occurring (not sure if
there could be valid use cases for such behaviour)? My proposal would be a
function like the following:
template <typename Iter, typename Iter_dest, typename Pred>
Iter move_if(Iter begin, Iter end, Iter_dest& dest, Pred pred)
{
Iter it_removed = end;
while (begin != end) {
if (pred(*begin)) {
*dest = std::move(*begin);
it_removed = begin++;
while (begin != end) {
if (pred(*begin)) {
*dest = std::move(*begin);
}
else {
*(it_removed++) = std::move(*begin);
}
++begin;
}
break;
}
++begin;
}
return it_removed;
}
template <typename Iter, typename Iter_dest, typename Pred>
Iter move_if(Iter begin, Iter end, Iter_dest&& dest, Pred pred)
{
return move_if(begin, end, dest, pred);
}
Where like std::remove_if(), it would return the position of where the
valid but unspecified objects begin, but unlike std::copy_if(), it would
have the dest parameter as a reference to an iterator, so that it can be
tracked if wanted after the call. If dest is a rvalue, then it would then
it's just use it as an lvalue as the value doesn't matter after the call
anyway. It's a simple addition, but one that could prevent nubes from
problems, and simplify more advanced users lives.
--
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/2474ec27-4d1b-4e0c-9456-fe3ee10b3ed2%40isocpp.org.
------=_Part_296_521857661.1554206196831
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><span style=3D"font-size: small;">I was looking around and=
found a few mentions about using=C2=A0</span><font face=3D"monospace, mono=
space" style=3D"font-size: small;">copy_if()</font><span style=3D"font-size=
: small;">=C2=A0with move iterators.=C2=A0 After thinking about this, I rea=
lised that this could leave valid but unspecified objects scattered around =
in the container, which could cause problems.=C2=A0 To stop this from happe=
ning, can there not be a=C2=A0</span><font face=3D"monospace, monospace" st=
yle=3D"font-size: small;"><span class=3D"il">std</span>::move_if()</font><s=
pan style=3D"font-size: small;">=C2=A0algorithm declared and=C2=A0</span><i=
style=3D"font-size: small;">maybe</i><span style=3D"font-size: small;">=C2=
=A0have=C2=A0</span><font face=3D"monospace, monospace" style=3D"font-size:=
small;"><span class=3D"il">std</span>::copy_if()</font><span style=3D"font=
-size: small;">=C2=A0deleted for move iterators to prevent such problems fr=
om occurring (not sure if there could be valid use cases for such behaviour=
)?=C2=A0 My proposal would be a function like the following:</span><br styl=
e=3D"font-size: small;"><div style=3D"font-size: small;"><br></div><div sty=
le=3D"font-size: small;"><div><font face=3D"monospace, monospace">template =
<typename Iter, typename Iter_dest, typename Pred></font></div><div><=
font face=3D"monospace, monospace">Iter move_if(Iter begin, Iter end, Iter_=
dest& dest, Pred pred)</font></div><div><font face=3D"monospace, monosp=
ace">{</font></div><div><font face=3D"monospace, monospace">=C2=A0 Iter it_=
removed =3D end;</font></div><div><font face=3D"monospace, monospace">=C2=
=A0 while (begin !=3D end) {</font></div><div><font face=3D"monospace, mono=
space">=C2=A0 =C2=A0 if (pred(*begin)) {</font></div><div><font face=3D"mon=
ospace, monospace">=C2=A0 =C2=A0 =C2=A0 *dest =3D=C2=A0<span class=3D"il">s=
td</span>::move(*begin);</font></div><div><font face=3D"monospace, monospac=
e">=C2=A0 =C2=A0 =C2=A0 it_removed =3D begin++;</font></div><div><font face=
=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 while (begin !=3D end) {</fo=
nt></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 if (pred(*begin)) {</font></div><div><font face=3D"monospace, monospace=
">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *dest =3D=C2=A0<span class=3D"il">std<=
/span>::move(*begin);</font></div><div><font face=3D"monospace, monospace">=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</font></div><div><font face=3D"monospace, mon=
ospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 else {</font></div><div><font face=3D"m=
onospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 *(it_removed++) =3D=
=C2=A0<span class=3D"il">std</span>::move(*begin);</font></div><div><font f=
ace=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</font></div><div=
><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 ++begin;</=
font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 }<=
/font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =C2=A0 b=
reak;=C2=A0=C2=A0</font></div><div><font face=3D"monospace, monospace">=C2=
=A0 =C2=A0 }</font></div><div><font face=3D"monospace, monospace">=C2=A0 =
=C2=A0 ++begin;</font></div><div><font face=3D"monospace, monospace">=C2=A0=
}</font></div><div><font face=3D"monospace, monospace">=C2=A0 return it_re=
moved;</font></div><div><font face=3D"monospace, monospace">}</font></div><=
div><font face=3D"monospace, monospace"><br></font></div><div><font face=3D=
"monospace, monospace">template <typename Iter, typename Iter_dest, type=
name Pred></font></div><div><font face=3D"monospace, monospace">Iter mov=
e_if(Iter begin, Iter end, Iter_dest&& dest, Pred pred)</font></div=
><div><font face=3D"monospace, monospace">{</font></div><div><font face=3D"=
monospace, monospace">=C2=A0 return move_if(begin, end, dest, pred);</font>=
</div><div><font face=3D"monospace, monospace">}</font></div><div><br></div=
><div>Where like=C2=A0<font face=3D"monospace, monospace"><span class=3D"il=
">std</span>::remove_if()</font>, it would return the position of where the=
valid but unspecified objects begin, but unlike=C2=A0<font face=3D"monospa=
ce, monospace"><span class=3D"il">std</span>::copy_if()</font>, it would ha=
ve the=C2=A0<font face=3D"monospace, monospace">dest</font>=C2=A0parameter =
as a reference to an iterator, so that it can be tracked if wanted after th=
e call.=C2=A0 If=C2=A0<font face=3D"monospace, monospace">dest</font>=C2=A0=
is a rvalue, then it would then it's just use it as an lvalue as the va=
lue doesn't matter after the call anyway.=C2=A0 It's a simple addit=
ion, but one that could prevent nubes from problems, and simplify more adva=
nced users lives.</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/2474ec27-4d1b-4e0c-9456-fe3ee10b3ed2%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/2474ec27-4d1b-4e0c-9456-fe3ee10b3ed2=
%40isocpp.org</a>.<br />
------=_Part_296_521857661.1554206196831--
------=_Part_295_664121668.1554206196831--
.