220 27933 <b627ed17-df02-4577-8a43-cc1af308673b@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: We need const_shared_ptr<T>
Date: Wed, 24 Aug 2016 17:54:51 -0700 (PDT)
Lines: 250
Approved: news@gmane.org
Message-ID: <b627ed17-df02-4577-8a43-cc1af308673b@isocpp.org>
References: <c3616c21-a33a-44b7-9912-b8ce927b57e9@isocpp.org>
 <444132ec-e8f3-400d-81b2-330a016702ee@isocpp.org>
 <45b8bdc7-5463-428a-aaef-d25ba0d2aa76@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_103_961162589.1472086491661"
X-Trace: blaine.gmane.org 1472086500 624 195.159.176.226 (25 Aug 2016 00:55:00 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Thu, 25 Aug 2016 00:55:00 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBXMD7G6QKGQEVYINTCI@isocpp.org Thu Aug 25 02:54:55 2016
Return-path: <std-proposals+bncBCEKFTV6ZUMBBXMD7G6QKGQEVYINTCI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f69.google.com ([209.85.220.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBXMD7G6QKGQEVYINTCI@isocpp.org>)
	id 1bciwY-00083d-B2
	for gclcip-std-proposals@m.gmane.org; Thu, 25 Aug 2016 02:54:54 +0200
Original-Received: by mail-pa0-f69.google.com with SMTP id vd14sf54739376pab.3
        for <gclcip-std-proposals@m.gmane.org>; Wed, 24 Aug 2016 17:54:55 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=JXNbKfcK4gqtWL/y5J/KRnCH/1h+6SVIFwccTbRG8WE=;
        b=eVghgjcx7n9GYGUu32aS8c2nafn0OUzWYW6WIsYNvgPAnMcRtw2Febkoit+GeMGYO8
         SXg/OCqfxAc1ngJm2OgFa9M9pgzagBkXCH/xd296pxiZHK00PLaXA5fFBpdp50Hq6Auw
         OWL5SiUtXObIwoKD8l1u0wh84ztoYr3rTNwG+tutV6pe02qBu+FdgFgOJA1f3N/s5q38
         O8q/Mg6zfwkwMhhkkOb8+dmo9aFYqCg9yofiHFz9hrCusGsZdG+/JlvFztOmLto1UrNF
         2lUt4HBDBSo4kn/6dcwgqqQGGgIlmicYztCeS8tjmcNZt4/qQewlAB2HrxSBRneTKn7w
         PsxA==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=JXNbKfcK4gqtWL/y5J/KRnCH/1h+6SVIFwccTbRG8WE=;
        b=GhcdMrACZWR+znMkdhkKwtaFWhrFc4rYPv0kxhHLe7r1Oo6VjiO8SPx1E7uNXru4N3
         InbVYxD9795Dx7YKoaggk1NpEPeDSloU9J48zTeNb6BKC+FRQS1A4L+YQwJYJQM4Y8nH
         cpLbDdIow7G430gPfu0aMbZ4PjYn8dAoEIkaAb80rhMrzdlr/e9hKOwvNBEx4+S6gHG0
         aE0qgnp+jREpCR+uk28mON15x0qWVUePnevL830sn1T7EbaeFkP7imRSfunte+o8H34r
         h6q2NHoZlr7hxUipcAu9hs2Idu5RdvBu/IGLOU3EcH8eVb0gldehYwuNuELYBmgQwmzg
         WzUg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=JXNbKfcK4gqtWL/y5J/KRnCH/1h+6SVIFwccTbRG8WE=;
        b=etlhlNcr9QBuu9UsZrddKdSu5JmYCwMsWqjY2pa3iluLCz4/I8ZbEQSskjC+XOeFUE
         OR2kzMC0F+AVh5wP6Xrkl+xM7qKKZuomMgZ20USFfipTlNbhUkExRnmC97d97DVGC+uC
         /vi69GANmh3PB5XyezNMwWSrhTorxyz/4biCaZMlCPTbAFxxLd+M2l8hW87f4jLoz1++
         eDHt4YMCQuG8ItiaGn2oybOlAOk71EEBcQQBPYQR7lW1HdlpWeTVWlkTwPgRx55vWbIL
         OGB8ybSzeyJ77L06rMHDd0zL4ZS9/tfa6790aP3Prm0HA6x6r6x3Ayul/IWmQQyjcOZ2
         HbUg==
X-Gm-Message-State: AE9vXwM5T6khwkT27t/JuK24q92DqLjTkg+0kEFvrUNkVJp0R78YgiTaiiWOlxklTMQ4qg==
X-Received: by 10.66.191.103 with SMTP id gx7mr4291718pac.37.1472086494751;
        Wed, 24 Aug 2016 17:54:54 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.130.145 with SMTP id m17ls44981ioi.32.gmail; Wed, 24 Aug
 2016 17:54:53 -0700 (PDT)
X-Received: by 10.36.111.147 with SMTP id x141mr120540itb.6.1472086492956;
        Wed, 24 Aug 2016 17:54:52 -0700 (PDT)
In-Reply-To: <45b8bdc7-5463-428a-aaef-d25ba0d2aa76@isocpp.org>
X-Original-Sender: jmckesson@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: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://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>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:27933
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/27933>

------=_Part_103_961162589.1472086491661
Content-Type: multipart/alternative; 
	boundary="----=_Part_104_1344369097.1472086491661"

------=_Part_104_1344369097.1472086491661
Content-Type: text/plain; charset=UTF-8

On Wednesday, August 24, 2016 at 7:57:33 PM UTC-4, Edward Catmur wrote:
>
> On Wednesday, 24 August 2016 15:45:12 UTC+1, Nicol Bolas  wrote: 
> > On Wednesday, August 24, 2016 at 3:39:33 AM UTC-4, Alexey Mamontov 
> wrote: 
> > 
> > There is an interesting concept of pointers pair: const_shared with 
> mutable_unique.  
> > 
> > Const shared is move-constructible from mutable_unique: this enqures 
> that the value we have a const shared pointer to can not be references for 
> edit from any other location (the problem that shared_ptr<const T> can't 
> solve). 
> > 
> > Why does it not solve this problem? Outside of `const_pointer_cast` or 
> similar gymnastics, if you have a `shared_ptr<const T>`, you cannot convert 
> it into a `shared_ptr<T>`. Oh sure, you can always `get` the pointer and 
> `const_cast` it. But your `const_shared_ptr` has no way to prevent that 
> either. 
>
> But there can be an already existing shared_ptr<T> aliasing your 
> shared_ptr<T const>.


Without a `const_cast` or similar? If you do `make_shared<const T>(...)`, I 
have no idea how you would get the aliasing to `shared_ptr<T>` without a 
`const_cast` or similar.

At least, not unless it's a pointer to a *different* `T`.
 

> The proposal here is that const_shared_ptr<T> can only be aliased by other 
> const_shared_ptr<T>s.
>
> > > mutable_unique is also move constructible from the const shared, while 
> the following logic is perfomed: if the source is the last reference, then 
> the same pointer is moved to the mutable instance, otherwise it creates a 
> copy using copy-constructor. This allowes to modify local instances while 
> paying the lowest possible price, and making copies only if there are some 
> other users of the same instance exist. 
> > 
> > I'm not sure I understand the conceptual foundation of what you're 
> asking for. I understand what all the pieces do; I just don't understand 
> the problem you're solving. In particular, why would constructing a 
> `mutable_unique` from a `const_shared_ptr` invoke copy construction? If 
> you're copying the value, then you're not accessing it by reference. So is 
> it a value type or a reference type? 
>
> In order to ensure that a const pointer cannot have its pointee 
> concurrently modified by a mutable pointer.
>

Except that it can. The OP made it clear that if you take the last 
`const_shared_ptr` and turn it into a `mutable_unique`, then the 
`mutable_unique` *doesn't* copy the value; it merely takes ownership of the 
pointer from the `const_shared_ptr`. So yes, it is possible to modify the 
supposedly const object.

Just not directly through `const_shared_ptr`.

Structurally, this all seems very much like a generalized COW, with the 
optimization that the last reference to the shared object doesn't need to 
copy when it tries to write to it.
 

> > Or is this some form of copy-on-write value semantics, generalized to 
> all types? If that's the idea behind this, then the names of these types 
> are completely wrong. They're not smart pointers; they're values. And the 
> fact that the const value is being "shared" is an implementation detail. 
>
> Well, other than the clone operation being an observable side effect. 
>
> > Indeed, I would even go so far as to say that, if this is the idea, then 
> giving it an existing object to govern is really the wrong interface. Such 
> a type should create the objects it manages internally; this would allow 
> the implementation to be more efficient (ala `make_shared`). That is, you 
> construct the mutable unique value type, then move the value into your 
> "const" COW shared object. 
> > 
> > So what you really have is `unique_value<T>` and `shared_value<const 
> T>`, with conversions between them. This also permits you to have a mutable 
> `shared_value<T>` if a user wants it, rather than enforcing immutability at 
> the interface level. 
>
> Would that allow enforcement of non modification by an aliasing value? 
> There's two sides to immutability; there's "I can't modify it" and there's 
> "no-one can modify it".
>

Conceptually, it's a value type, so it wouldn't have aliasing of any form. 
What would that even mean? Would you be aliasing a value with another 
value? Indeed, the OP excluded aliasing from `const_shared_ptr`'s 
interface, since it doesn't make sense.

After all, if your `const_shared_ptr<T>` actually owns some object of type 
`U`, what would it mean to make a `mutable_unique` copy? Are you asking to 
make a copy of the aliased pointer or the owned object? The latter is a bit 
problematic, since it has been type-erased by that point.

> Of course, that also raises the question of a `weak_ptr` equivalent. Does 
> this type need such a thing? With COW strings, the question was moot; 
> strings are dumb arrays, so there was never any question of whether a COW 
> string could have a circular reference to itself. But with generalized COW 
> objects, that possibility now exists. 
>
> Would such a weak_value<T const> be invalidated if all the shared_value<T 
> const>s are converted to mutable?


They would have to be, since there are no shared values out there for it to 
reference.

-- 
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/b627ed17-df02-4577-8a43-cc1af308673b%40isocpp.org.

------=_Part_104_1344369097.1472086491661
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Wednesday, August 24, 2016 at 7:57:33 PM UTC-4, Edward =
Catmur wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-le=
ft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Wednesday, 24 =
August 2016 15:45:12 UTC+1, Nicol Bolas =C2=A0wrote:
<br>&gt; On Wednesday, August 24, 2016 at 3:39:33 AM UTC-4, Alexey Mamontov=
 wrote:
<br>&gt;=20
<br>&gt; There is an interesting concept of pointers pair: const_shared=C2=
=A0with mutable_unique.=C2=A0
<br>&gt;=20
<br>&gt; Const shared is move-constructible from mutable_unique: this enqur=
es that the value we have a const shared=C2=A0pointer to can not be referen=
ces for edit from any other location (the problem=C2=A0that shared_ptr&lt;c=
onst T&gt; can&#39;t solve).
<br>&gt;=20
<br>&gt; Why does it not solve this problem? Outside of `const_pointer_cast=
` or similar gymnastics, if you have a `shared_ptr&lt;const T&gt;`, you can=
not convert it into a `shared_ptr&lt;T&gt;`. Oh sure, you can always `get` =
the pointer and `const_cast` it. But your `const_shared_ptr` has no way to =
prevent that either.
<br>
<br>But there can be an already existing shared_ptr&lt;T&gt; aliasing your =
shared_ptr&lt;T const&gt;.</blockquote><div><br>Without a `const_cast` or s=
imilar? If you do `make_shared&lt;const T&gt;(...)`, I have no idea how you=
 would get the aliasing to `shared_ptr&lt;T&gt;` without a `const_cast` or =
similar.<br><br>At least, not unless it&#39;s a pointer to a <i>different</=
i> `T`.<br>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0=
;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">The pro=
posal here is that const_shared_ptr&lt;T&gt; can only be aliased by other c=
onst_shared_ptr&lt;T&gt;s.<br>
<br>&gt; &gt; mutable_unique is also move constructible from the const shar=
ed, while the following logic is perfomed: if the source is the last refere=
nce, then the same pointer is moved to the mutable instance, otherwise it c=
reates a copy using copy-constructor. This allowes to modify local instance=
s while paying the lowest possible price, and making copies only if there a=
re some other users of the same instance exist.
<br>&gt;=20
<br>&gt; I&#39;m not sure I understand the conceptual foundation of what yo=
u&#39;re asking for. I understand what all the pieces do; I just don&#39;t =
understand the problem you&#39;re solving. In particular, why would constru=
cting a `mutable_unique` from a `const_shared_ptr` invoke copy construction=
? If you&#39;re copying the value, then you&#39;re not accessing it by refe=
rence. So is it a value type or a reference type?
<br>
<br>In order to ensure that a const pointer cannot have its pointee concurr=
ently modified by a mutable pointer.<br></blockquote><div><br>Except that i=
t can. The OP made it clear that if you take the last `const_shared_ptr` an=
d turn it into a `mutable_unique`, then the `mutable_unique` <i>doesn&#39;t=
</i> copy the value; it merely takes ownership of the pointer from the `con=
st_shared_ptr`. So yes, it is possible to modify the supposedly const objec=
t.<br><br>Just not directly through `const_shared_ptr`.<br><br>Structurally=
, this all seems very much like a generalized COW, with the optimization th=
at the last reference to the shared object doesn&#39;t need to copy when it=
 tries to write to it.<br>=C2=A0</div><blockquote class=3D"gmail_quote" sty=
le=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left=
: 1ex;">
&gt; Or is this some form of copy-on-write value semantics, generalized to =
all types? If that&#39;s the idea behind this, then the names of these type=
s are completely wrong. They&#39;re not smart pointers; they&#39;re values.=
 And the fact that the const value is being &quot;shared&quot; is an implem=
entation detail.
<br>
<br>Well, other than the clone operation being an observable side effect.=
=20
<br>
<br>&gt; Indeed, I would even go so far as to say that, if this is the idea=
, then giving it an existing object to govern is really the wrong interface=
.. Such a type should create the objects it manages internally; this would a=
llow the implementation to be more efficient (ala `make_shared`). That is, =
you construct the mutable unique value type, then move the value into your =
&quot;const&quot; COW shared object.
<br>&gt;=20
<br>&gt; So what you really have is `unique_value&lt;T&gt;` and `shared_val=
ue&lt;const T&gt;`, with conversions between them. This also permits you to=
 have a mutable `shared_value&lt;T&gt;` if a user wants it, rather than enf=
orcing immutability at the interface level.
<br>
<br>Would that allow enforcement of non modification by an aliasing value? =
There&#39;s two sides to immutability; there&#39;s &quot;I can&#39;t modify=
 it&quot; and there&#39;s &quot;no-one can modify it&quot;.<br></blockquote=
><div><br>Conceptually, it&#39;s a value type, so it wouldn&#39;t have alia=
sing of any form. What would that even mean? Would you be aliasing a value =
with another value? Indeed, the OP excluded aliasing from `const_shared_ptr=
`&#39;s interface, since it doesn&#39;t make sense.<br><br>After all, if yo=
ur `const_shared_ptr&lt;T&gt;` actually owns some object of type `U`, what =
would it mean to make a `mutable_unique` copy? Are you asking to make a cop=
y of the aliased pointer or the owned object? The latter is a bit problemat=
ic, since it has been type-erased by that point.<br><br></div><blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
 #ccc solid;padding-left: 1ex;">
&gt; Of course, that also raises the question of a `weak_ptr` equivalent. D=
oes this type need such a thing? With COW strings, the question was moot; s=
trings are dumb arrays, so there was never any question of whether a COW st=
ring could have a circular reference to itself. But with generalized COW ob=
jects, that possibility now exists.
<br>
<br>Would such a weak_value&lt;T const&gt; be invalidated if all the shared=
_value&lt;T const&gt;s are converted to mutable?</blockquote><div><br>They =
would have to be, since there are no shared values out there for it to refe=
rence.<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/b627ed17-df02-4577-8a43-cc1af308673b%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/b627ed17-df02-4577-8a43-cc1af308673b=
%40isocpp.org</a>.<br />

------=_Part_104_1344369097.1472086491661--

------=_Part_103_961162589.1472086491661--

.
