220 15708 <CAGg_6+Ms01u9BKaUNC4iPLg=_DCP0h4xQQ7fySbSu6zEVgJj4Q@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Nevin Liber <nevin@eviloverlord.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Fwd: vector<T*> vs vector<unique_ptr<T>> problems
Date: Fri, 16 Jan 2015 13:10:49 -0600
Lines: 200
Approved: news@gmane.org
Message-ID: <CAGg_6+Ms01u9BKaUNC4iPLg=_DCP0h4xQQ7fySbSu6zEVgJj4Q@mail.gmail.com>
References: <d6dcd5e7-aeb9-440a-bea9-dae8c9a90074@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=089e0158b50c70b6f5050cc9bde0
X-Trace: ger.gmane.org 1421435494 23211 80.91.229.3 (16 Jan 2015 19:11:34 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 16 Jan 2015 19:11:34 +0000 (UTC)
To: "std-proposals@isocpp.org" <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCE35H5S6IDBBYWE4WSQKGQEUCTL7VY@isocpp.org Fri Jan 16 20:11:34 2015
Return-path: <std-proposals+bncBCE35H5S6IDBBYWE4WSQKGQEUCTL7VY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ob0-f198.google.com ([209.85.214.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCE35H5S6IDBBYWE4WSQKGQEUCTL7VY@isocpp.org>)
	id 1YCCIu-0003Zy-1f
	for gclcip-std-proposals@m.gmane.org; Fri, 16 Jan 2015 20:11:32 +0100
Original-Received: by mail-ob0-f198.google.com with SMTP id vb8sf119544892obc.1
        for <gclcip-std-proposals@m.gmane.org>; Fri, 16 Jan 2015 11:11:31 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:sender:in-reply-to:references:from
         :date:message-id:subject:to:content-type:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe;
        bh=yWrJs9oDsVdu/7u1DxioKcD5q2c5XKCc1giPEq+XlzQ=;
        b=AZvWIQV6evJpdfx6UDWUMaQ1C8aRZyQg1Aew7JUxvSNHI0L0dTsbw5kwzNPIiLYkRS
         xbEhpXGaa6osQdM/n9iFHN+BLodoUTGiG1Sz/lF9OJFSy62JEqPkW5QgexrbrEaXVCyg
         +mn/Zd1nL/32iBnwjSCBdb2na7J9XJVJLxn1t4v1KsOjcQeld7GTtNle1OxtypYnFzWM
         QhxXInUCOvSsLBwyhg4vsauBLcnaoX3zu7ZGg2n54NmH8gDXsLs3K9VbuRsMQtrNFecr
         UL09+u3gEFG3HNjHBpq5nJDmYv7/Fd39rz/YM5twVeAlq9RrFT33SZeqBOPj/rxMHd7k
         frsw==
X-Gm-Message-State: ALoCoQnhZAf4rAfrXVEU7uVhDqzf4N11mgse0FC4Z95pDDh11DIRnX0AxFi4Z1audjy2ljjlwaRK
X-Received: by 10.42.22.145 with SMTP id o17mr13990690icb.17.1421435491094;
        Fri, 16 Jan 2015 11:11:31 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.106.201 with SMTP id e67ls1512272qgf.63.gmail; Fri, 16 Jan
 2015 11:11:30 -0800 (PST)
X-Received: by 10.229.70.133 with SMTP id d5mr27937757qcj.2.1421435490141;
        Fri, 16 Jan 2015 11:11:30 -0800 (PST)
Original-Received: from mail-yk0-x22b.google.com (mail-yk0-x22b.google.com. [2607:f8b0:4002:c07::22b])
        by mx.google.com with ESMTPS id a11si7071208qge.86.2015.01.16.11.11.29
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 16 Jan 2015 11:11:29 -0800 (PST)
Received-SPF: pass (google.com: domain of nliber@gmail.com designates 2607:f8b0:4002:c07::22b as permitted sender) client-ip=2607:f8b0:4002:c07::22b;
Original-Received: by mail-yk0-f171.google.com with SMTP id 142so10278995ykq.2
        for <std-proposals@isocpp.org>; Fri, 16 Jan 2015 11:11:29 -0800 (PST)
X-Received: by 10.236.29.17 with SMTP id h17mr10635147yha.188.1421435489791;
 Fri, 16 Jan 2015 11:11:29 -0800 (PST)
Original-Sender: nliber@gmail.com
Original-Received: by 10.170.85.8 with HTTP; Fri, 16 Jan 2015 11:10:49 -0800 (PST)
In-Reply-To: <d6dcd5e7-aeb9-440a-bea9-dae8c9a90074@isocpp.org>
X-Original-Sender: nevin@eviloverlord.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of nliber@gmail.com designates 2607:f8b0:4002:c07::22b as permitted
 sender) smtp.mail=nliber@gmail.com;       dkim=pass header.i=@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:15708
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/15708>

--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">&lt;<a href=3D"mailto:fmatthew5876@gmail.com" target=3D"_blank">fm=
atthew5876@gmail.com</a>&gt;</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&lt;T[]&gt; and vector&lt;unique_ptr&lt;T&gt;&gt=
; 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)">&lt;</span><span styl=
e=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(102,102,0)">*&gt;</=
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)">&lt;</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)">&lt;=
</span><span style=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(10=
2,102,0)">&gt;&gt;</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)">&lt;</span><span style=
=3D"color:rgb(0,0,0)">T</span><span style=3D"color:rgb(102,102,0)">*&gt;</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)">&lt;</span><span style=3D"color:rgb(0,0,0)">T</span><span s=
tyle=3D"color:rgb(102,102,0)">*&gt;</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&lt;unique_ptr&lt;T&gt;&gt=
;, we are forced to create a copy because of the type system.</div><div>=C2=
=A0</div><div>With vector&lt;T*&gt;, 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&#39;t you just use vector&lt;shared_ptr&lt;=
T&gt;&gt;?=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&lt;T*&gt;(std::unique_ptr&lt;T&gt;()) 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&#39;t see why this c=
ase is different than any other case where you have a vector&lt;A&gt; and w=
ant to return a range&lt;B&gt;.=C2=A0 I don&#39;t see that the unique_ptr&l=
t;T&gt; -&gt; T* case is all that special (or even common).<br></div></div>=
-- <br><div class=3D"gmail_signature">=C2=A0Nevin &quot;:-)&quot; Liber=C2=
=A0 &lt;mailto:<a href=3D"mailto:nevin@eviloverlord.com" target=3D"_blank">=
nevin@eviloverlord.com</a>&gt;=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&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 />

--089e0158b50c70b6f5050cc9bde0--

.
