Topic: Informal request for comments: back_inserter() with


Author: Janky Ferenc <fecjanky@gmail.com>
Date: Mon, 25 Sep 2017 15:30:37 -0700 (PDT)
Raw View
------=_Part_6489_660816812.1506378637723
Content-Type: multipart/alternative;
 boundary="----=_Part_6490_1094247603.1506378637724"

------=_Part_6490_1094247603.1506378637724
Content-Type: text/plain; charset="UTF-8"

Hey,

I know with all the Range TS at our doorstep this topic might seem a bit
outdated, but there are a lot of code in "legacy environment"  where the
penetration of something like Ranges would take at least another 2 years
after becoming standard but still could make use of a simple convenience
feature. A typical setting in such environment is that std::back_inserter()
has an std::vector<T> as its destination container which is usually
reserved just right before using the back insert iterator as an input
argument to some STL algorithm call.


....
std::vector<T> out;
out.reserve(in.size());
std::transform(std::begin(in),std::end(in),std::back_inserter(out),
    [](const auto& elem){
        /*create something from elem...*/;
        return something;
    });
....


I would like to create a proposal about adding an std::back inserter
function template overload with an additional container::size_type
parameter, which if the destination container supports reserve, invokes
that before returning with the back insert iterator -  something like:

template< class Container >
std::back_insert_iterator<Container> back_inserter( Container& c, typename
Container::size_type size_hint)
{
    constexpr if(/*c.reserve(size_hint) is callable */){
        c.reserve(c.size() + size_hint);
    }
    return back_inserter(c);
}



Having that the original example could be rewritten to:

....
std::vector<T> out;
std::transform(std::begin(in),std::end(in),std::back_inserter(out,in.size()),
    [](const auto& elem){
        /*create something from elem...*/;
        return something;
    });
....

Any feedback is appreciated.

Thanks & BR,

Ferenc


--
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/56d9ff76-ebe4-4fe0-8099-954cb29d49fa%40isocpp.org.

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

<div dir=3D"ltr"><div>Hey,</div><div><br></div><div>I know with all the Ran=
ge TS at our doorstep this topic might seem a bit outdated, but there are a=
 lot of code in &quot;legacy environment&quot; =C2=A0where the penetration =
of something like Ranges would take at least another 2 years after becoming=
 standard but still could make use of a simple convenience feature. A typic=
al setting in such environment is that std::back_inserter() has an std::vec=
tor&lt;T&gt; as its destination container which is usually reserved just ri=
ght before using the back insert iterator as an input argument to some STL =
algorithm call.</div><div><br></div><div><div class=3D"prettyprint" style=
=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187);=
 border-style: solid; border-width: 1px; word-wrap: break-word;"><code clas=
s=3D"prettyprint"><div class=3D"subprettyprint"><br></div><div class=3D"sub=
prettyprint"><div style=3D"font-family: Arial, Helvetica, sans-serif; backg=
round-color: rgb(255, 255, 255);">...</div><div style=3D"font-family: Arial=
, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">std::vector=
&lt;T&gt; out;</div><div style=3D"font-family: Arial, Helvetica, sans-serif=
; background-color: rgb(255, 255, 255);">out.reserve(in.size());</div><div =
style=3D"font-family: Arial, Helvetica, sans-serif; background-color: rgb(2=
55, 255, 255);">std::transform(std::begin(in),std::end(in),std::back_insert=
er(out),</div><div style=3D"font-family: Arial, Helvetica, sans-serif; back=
ground-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 [](const auto&amp; elem){<=
/div><div style=3D"font-family: Arial, Helvetica, sans-serif; background-co=
lor: rgb(255, 255, 255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0 /*create something fr=
om elem...*/;</div><div style=3D"font-family: Arial, Helvetica, sans-serif;=
 background-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0 return =
something;</div><div style=3D"font-family: Arial, Helvetica, sans-serif; ba=
ckground-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 });</div><div style=3D"f=
ont-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 2=
55);">...</div></div></code></div><br></div><div><br></div><div>I would lik=
e to create a proposal about adding an std::back inserter function template=
 overload with an additional container::size_type parameter, which if the d=
estination container supports reserve, invokes that before returning with t=
he back insert iterator - =C2=A0something like:</div><div><br></div><div><d=
iv class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bor=
der-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word=
-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprin=
t"><div style=3D"font-family: Arial, Helvetica, sans-serif; background-colo=
r: rgb(255, 255, 255);">template&lt; class Container &gt;</div><div style=
=3D"font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 2=
55, 255);">std::back_insert_iterator&lt;Container&gt; back_inserter( Contai=
ner&amp; c, typename Container::size_type size_hint)</div><div style=3D"fon=
t-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255=
);">{</div><div style=3D"font-family: Arial, Helvetica, sans-serif; backgro=
und-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 constexpr if(/*c.reserve(size=
_hint) is callable */){</div><div style=3D"font-family: Arial, Helvetica, s=
ans-serif; background-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 c.reserve(c.size() + size_hint);</div><div style=3D"font-family: Arial,=
 Helvetica, sans-serif; background-color: rgb(255, 255, 255);">=C2=A0 =C2=
=A0 }</div><div style=3D"font-family: Arial, Helvetica, sans-serif; backgro=
und-color: rgb(255, 255, 255);">=C2=A0 =C2=A0 return back_inserter(c);</div=
><div style=3D"font-family: Arial, Helvetica, sans-serif; background-color:=
 rgb(255, 255, 255);">}</div><div><br></div></div></code></div><br><br></di=
v><div>Having that the original example could be rewritten to:</div><div><b=
r></div><div><div class=3D"prettyprint" style=3D"background-color: rgb(250,=
 250, 250); border-color: rgb(187, 187, 187); border-style: solid; border-w=
idth: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><div style=3D"font-family: Arial, Helvetica, sans-serif=
; background-color: rgb(255, 255, 255);">...</div><div style=3D"font-family=
: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">std:=
:vector&lt;T&gt; out;</div><div style=3D"font-family: Arial, Helvetica, san=
s-serif; background-color: rgb(255, 255, 255);">std::transform(std::begin(i=
n),std::end(in),std::back_inserter(out,in.size()),</div><div style=3D"font-=
family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);=
">=C2=A0 =C2=A0 [](const auto&amp; elem){</div><div style=3D"font-family: A=
rial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 /*create something from elem...*/;</div><div style=3D"=
font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, =
255);">=C2=A0 =C2=A0 =C2=A0 =C2=A0 return something;</div><div style=3D"fon=
t-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255=
);">=C2=A0 =C2=A0 });</div><div style=3D"font-family: Arial, Helvetica, san=
s-serif; background-color: rgb(255, 255, 255);">...</div></div></code></div=
></div><div><br></div><div>Any feedback is appreciated.</div><div><br></div=
><div>Thanks &amp; BR,</div><div><br></div><div>Ferenc</div><div><br></div>=
<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&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/56d9ff76-ebe4-4fe0-8099-954cb29d49fa%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/56d9ff76-ebe4-4fe0-8099-954cb29d49fa=
%40isocpp.org</a>.<br />

------=_Part_6490_1094247603.1506378637724--

------=_Part_6489_660816812.1506378637723--

.