Topic: Fwd: vector<T*> vs vector<unique_ptr<T>> problems
Author: Nevin Liber <nevin@eviloverlord.com>
Date: Fri, 16 Jan 2015 13:10:49 -0600
Raw View
--089e0158b50c70b6f5050cc9bde0
Content-Type: text/plain; charset=UTF-8
On 14 January 2015 at 17:55, Matthew Fioravante <fmatthew5876@gmail.com>
wrote:
> We have unique_ptr<T[]> and vector<unique_ptr<T>> but there is one big
> use case where both are inadequate.
>
>
> struct FooSlow {
> std::vector<T*> getSubset(int i) const { return { _v.begin() + i, _v.end
> () }; }
> std::vector<std::unique_ptr<T>> _v;
> };
>
> struct FooFast {
> carray_view<T*> getSubset(int i) const { return { _v.begin() + 1, _v.end
> () }; }
> std::vector<T*> _v;
>
> ~FooFast() { while(!_v.empty()) { delete v.back(); v.pop_back(); } }
> };
>
>
> Here we have a linear array of pointers in our Foo object and want to
> return a view to a subsection of the array. With vector<unique_ptr<T>>, we
> are forced to create a copy because of the type system.
>
> With vector<T*>, we can directly return a view referencing a subset but
> now we have to take care of copy(), copy=, move(), move=, and the
> destructor.
>
Given that you said this is a big use case already, how are you dealing
with copying FooSlow objects?
If you really want to do something like this, why can't you just use
vector<shared_ptr<T>>? You at least get the pointer syntax with any range
you return.
> Another alternative would be to have some guarantee in the standard that
> reinterpret_cast<T*>(std::unique_ptr<T>()) is valid.
>
Seems extremely unlikely C++ will move in this direction.
I don't see why this case is different than any other case where you have a
vector<A> and want to return a range<B>. I don't see that the
unique_ptr<T> -> T* case is all that special (or even common).
--
Nevin ":-)" Liber <mailto:nevin@eviloverlord.com> (847) 691-1404
--
---
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/.
--089e0158b50c70b6f5050cc9bde0
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On 14 January 2015 at 17:55, Matthew Fioravante <span dir=
=3D"ltr"><<a href=3D"mailto:fmatthew5876@gmail.com" target=3D"_blank">fm=
atthew5876@gmail.com</a>></span> wrote:<br><div class=3D"gmail_extra"><d=
iv class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margin:=
0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><d=
iv>=C2=A0We have unique_ptr<T[]> and vector<unique_ptr<T>>=
; but there is one big use case where both are inadequate.</div><div>=C2=A0=
</div><div>=C2=A0</div><div style=3D"border:1px solid rgb(187,187,187);word=
-wrap:break-word;background-color:rgb(250,250,250)"><code><div><font color=
=3D"#000088"><span style=3D"color:rgb(0,0,136)">struct</span></font><span s=
tyle=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)">FooS=
low</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb=
(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 std</span>=
<span style=3D"color:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,=
0)">vector</span><span style=3D"color:rgb(102,102,0)"><</span><span styl=
e=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(102,102,0)">*></=
span><span style=3D"color:rgb(0,0,0)"> getSubset</span><span style=3D"color=
:rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,136)">int</span><span=
style=3D"color:rgb(0,0,0)"> i</span><span style=3D"color:rgb(102,102,0)">)=
</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,=
0,136)">const</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D=
"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </span><sp=
an style=3D"color:rgb(0,0,136)">return</span><span style=3D"color:rgb(0,0,0=
)"> </span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"colo=
r:rgb(0,0,0)"> _v</span><span style=3D"color:rgb(102,102,0)">.</span><span =
style=3D"color:rgb(0,0,136)">begin</span><span style=3D"color:rgb(102,102,0=
)">()</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:r=
gb(102,102,0)">+</span><span style=3D"color:rgb(0,0,0)"> i</span><span styl=
e=3D"color:rgb(102,102,0)">,</span><span style=3D"color:rgb(0,0,0)"> _v</sp=
an><span style=3D"color:rgb(102,102,0)">.</span><span style=3D"color:rgb(0,=
0,136)">end</span><span style=3D"color:rgb(102,102,0)">()</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">};</span=
><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,=
0)">}</span><span style=3D"color:rgb(0,0,0)"><br></span><font color=3D"#666=
600"><span style=3D"color:rgb(0,0,0)">=C2=A0 std</span><span style=3D"color=
:rgb(102,102,0)">::</span><span style=3D"color:rgb(0,0,0)">vector</span><sp=
an style=3D"color:rgb(102,102,0)"><</span><span style=3D"color:rgb(0,0,0=
)">std</span><span style=3D"color:rgb(102,102,0)">::</span><span style=3D"c=
olor:rgb(0,0,0)">unique_ptr</span><span style=3D"color:rgb(102,102,0)"><=
</span><span style=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(10=
2,102,0)">>></span><span style=3D"color:rgb(0,0,0)"> _v</span><span s=
tyle=3D"color:rgb(102,102,0)">;</span><span style=3D"color:rgb(0,0,0)"><br>=
</span><span style=3D"color:rgb(102,102,0)">};</span><span style=3D"color:r=
gb(0,0,0)"><br><br></span><span style=3D"color:rgb(0,0,136)">struct</span><=
span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,0,102)=
">FooFast</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"col=
or:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 carr=
ay_view</span><span style=3D"color:rgb(102,102,0)"><</span><span style=
=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(102,102,0)">*></s=
pan><span style=3D"color:rgb(0,0,0)"> getSubset</span><span style=3D"color:=
rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,136)">int</span><span =
style=3D"color:rgb(0,0,0)"> i</span><span style=3D"color:rgb(102,102,0)">)<=
/span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0=
,136)">const</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"=
color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </span><spa=
n style=3D"color:rgb(0,0,136)">return</span><span style=3D"color:rgb(0,0,0)=
"> </span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color=
:rgb(0,0,0)"> _v</span><span style=3D"color:rgb(102,102,0)">.</span><span s=
tyle=3D"color:rgb(0,0,136)">begin</span><span style=3D"color:rgb(102,102,0)=
">()</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rg=
b(102,102,0)">+</span><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(0,102,102)">1</span><span style=3D"color:rgb(102,102,0)">,</s=
pan><span style=3D"color:rgb(0,0,0)"> _v</span><span style=3D"color:rgb(102=
,102,0)">.</span><span style=3D"color:rgb(0,0,136)">end</span><span style=
=3D"color:rgb(102,102,0)">()</span><span style=3D"color:rgb(0,0,0)"> </span=
><span style=3D"color:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0=
,0)"> </span><span style=3D"color:rgb(102,102,0)">}</span><span style=3D"co=
lor:rgb(0,0,0)"><br>=C2=A0 std</span><span style=3D"color:rgb(102,102,0)">:=
:</span><span style=3D"color:rgb(0,0,0)">vector</span><span style=3D"color:=
rgb(102,102,0)"><</span><span style=3D"color:rgb(0,0,0)">T</span><span s=
tyle=3D"color:rgb(102,102,0)">*></span><span style=3D"color:rgb(0,0,0)">=
_v</span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color=
:rgb(0,0,0)"><br><br>=C2=A0 </span><span style=3D"color:rgb(102,102,0)">~</=
span><span style=3D"color:rgb(102,0,102)">FooFast</span><span style=3D"colo=
r:rgb(102,102,0)">()</span><span style=3D"color:rgb(0,0,0)"> </span><span s=
tyle=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,0)"> </s=
pan><span style=3D"color:rgb(0,0,136)">while</span><span style=3D"color:rgb=
(102,102,0)">(!</span><span style=3D"color:rgb(0,0,0)">_v</span><span style=
=3D"color:rgb(102,102,0)">.</span><span style=3D"color:rgb(0,0,0)">empty</s=
pan><span style=3D"color:rgb(102,102,0)">())</span><span style=3D"color:rgb=
(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">{</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0,0,136)">delete</sp=
an><span style=3D"color:rgb(0,0,0)"> v</span><span style=3D"color:rgb(102,1=
02,0)">.</span><span style=3D"color:rgb(0,0,0)">back</span><span style=3D"c=
olor:rgb(102,102,0)">();</span><span style=3D"color:rgb(0,0,0)"> v</span><s=
pan style=3D"color:rgb(102,102,0)">.</span><span style=3D"color:rgb(0,0,0)"=
>pop_back</span><span style=3D"color:rgb(102,102,0)">();</span><span style=
=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0)">}</span>=
<span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102,102,0=
)">}</span><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color=
:rgb(102,102,0)">};</span><span style=3D"color:rgb(0,0,0)"><br></span></fon=
t><span style=3D"color:rgb(0,0,0)"><br></span></div></code></div><div><br>H=
ere we have a linear array of pointers in our Foo object and want to return=
a view to a subsection of the array. With vector<unique_ptr<T>>=
;, we are forced to create a copy because of the type system.</div><div>=C2=
=A0</div><div>With vector<T*>, we can directly return a view referenc=
ing a subset but now we have to=C2=A0take=C2=A0care of=C2=A0copy(), copy=3D=
, move(), move=3D, and the destructor.</div></div></blockquote><div><br></d=
iv><div>Given that you said this is a big use case already, how are you dea=
ling with copying FooSlow objects?<br><br></div><div>If you really want to =
do something like this, why can't you just use vector<shared_ptr<=
T>>?=C2=A0 You at least get the pointer syntax with any range you ret=
urn.<br></div><div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"m=
argin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"l=
tr"><div>=C2=A0 Another alternative would be to have some guarantee in the =
standard that reinterpret_cast<T*>(std::unique_ptr<T>()) is val=
id.</div></div></blockquote><div><br></div><div>Seems extremely unlikely C+=
+ will move in this direction.<br><br></div><div>I don't see why this c=
ase is different than any other case where you have a vector<A> and w=
ant to return a range<B>.=C2=A0 I don't see that the unique_ptr&l=
t;T> -> T* case is all that special (or even common).<br></div></div>=
-- <br><div class=3D"gmail_signature">=C2=A0Nevin ":-)" Liber=C2=
=A0 <mailto:<a href=3D"mailto:nevin@eviloverlord.com" target=3D"_blank">=
nevin@eviloverlord.com</a>>=C2=A0 (847) 691-1404</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" 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 />
--089e0158b50c70b6f5050cc9bde0--
.