Topic: std::vector (and other containers) to have a perfect


Author: leni536@gmail.com
Date: Wed, 11 Nov 2015 00:52:01 -0800 (PST)
Raw View
------=_Part_3523_1221868456.1447231921447
Content-Type: multipart/alternative;
 boundary="----=_Part_3524_159075979.1447231921447"

------=_Part_3524_159075979.1447231921447
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi,

I didn't find a topic like this, so here it is. Something like this would=
=20
be nice:

template <class... Args>
vector(size_type n,Args&&... args);

It would perfect forward args to value_type's constructor. Since vector=20
uses its allocator to construct, and in C++11 std::allocator already has a=
=20
perfect forwarding "construct" member function, it could use that. The only=
=20
problem I see that it would hijack other constructors so maybe it would=20
need tag dispatching to work.

vector(size_type n,vector::perfect_forward_type,Args&&... args);

I see that this is a bit uglier, but at least it would work.

Use cases:

1) It could be faster than vector(n,value_type(args)) if value_type's copy=
=20
constructor is more expensive than its constructor with "args" parameters=
=20
(I don't know a good example though).
2) If value_type's different constructors are not deterministic then maybe=
=20
it's desirable to call the constructor multiple times instead of copying=20
one constructed element. One example if value_type has a constructor taking=
=20
a random generator as an argument.

Workarounds:
1) Using a custom allocator. Problem: your allocator type is now stuck with=
=20
your vector instance. Maybe one would initialize the vector with a custom=
=20
allocator but wouldn't mind if std::allocator would be used in the future=
=20
uses of the same vector.
2) Construct an empty vector, use reserve then emplace_back newly=20
constructed elements one by one using value_type's constructor of choice.=
=20
I'm not sure if this would be equally efficient.

Best regards,
L=C3=A9n=C3=A1rd Szolnoki



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

------=_Part_3524_159075979.1447231921447
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br></div><div>I didn&#39;t find a topic like this=
, so here it is. Something like this would be nice:</div><div><br></div><di=
v><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187);=
 word-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=
=3D"prettyprint"><div class=3D"subprettyprint"><font color=3D"#660066"><spa=
n style=3D"color: #008;" class=3D"styled-by-prettify">template</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">class</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">...</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #606;" class=3D"styl=
ed-by-prettify">Args</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">&gt;</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"><br>vector</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">size_=
type n</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,</s=
pan><span style=3D"color: #606;" class=3D"styled-by-prettify">Args</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&amp;&amp;...</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> args</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">);</span></font></div=
></code></div><br>It would perfect forward args to value_type&#39;s constru=
ctor. Since vector uses its allocator to construct, and in C++11 std::alloc=
ator already has a perfect forwarding &quot;construct&quot; member function=
, it could use that. The only problem I see that it would hijack other cons=
tructors so maybe it would need tag dispatching to work.</div><div><br></di=
v><div><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, =
187); word-wrap: break-word; background-color: rgb(250, 250, 250);"><code c=
lass=3D"prettyprint"><div class=3D"subprettyprint"><font color=3D"#660066">=
<span style=3D"color: #000;" class=3D"styled-by-prettify">vector</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">size_type n</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">vector</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify">perfect_forward_type</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #606;" c=
lass=3D"styled-by-prettify">Args</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&amp;&amp;...</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> args</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">);</span></font></div></code></div><br>I see that t=
his is a bit uglier, but at least it would work.</div><div><br></div><div>U=
se cases:</div><div><br></div><div>1) It could be faster than vector(n,valu=
e_type(args)) if value_type&#39;s copy constructor is more expensive than i=
ts constructor with &quot;args&quot; parameters (I don&#39;t know a good ex=
ample though).</div><div>2) If value_type&#39;s different constructors are =
not deterministic then maybe it&#39;s desirable to call the constructor mul=
tiple times instead of copying one constructed element. One example if valu=
e_type has a constructor taking a random generator as an argument.</div><di=
v><br></div><div>Workarounds:</div><div>1) Using a custom allocator. Proble=
m: your allocator type is now stuck with your vector instance. Maybe one wo=
uld initialize the vector with a custom allocator but wouldn&#39;t mind if =
std::allocator would be used in the future uses of the same vector.</div><d=
iv>2) Construct an empty vector, use reserve then emplace_back newly constr=
ucted elements one by one using value_type&#39;s constructor of choice. I&#=
39;m not sure if this would be equally efficient.</div><div><br></div><div>=
Best regards,</div><div>L=C3=A9n=C3=A1rd Szolnoki</div><div><br></div><div>=
<br></div><div><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_3524_159075979.1447231921447--
------=_Part_3523_1221868456.1447231921447--

.