Topic: Add std::make_shared template specialization for
Author: Andrew Dunn <ANDREW.A.DUNN@leidos.com>
Date: Fri, 10 Oct 2014 15:31:42 +1100
Raw View
<html>
<head>
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-8=
">
</head>
<body bgcolor=3D"#FFFFFF" text=3D"#000000">
At the moment, <tt>std::make_shared</tt> will call the constructor
of an instance descended from <tt>std::enable_shared_from_this</tt>
before initialising the self referential <tt>weak_ptr</tt>.<br>
<br>
This makes it impossible to use <tt>shared_from_this()</tt> during
the construction phase of the instance. The use case which I
encountered is when constructing an instance which has member
variables that keep a weak reference to the parent instance.<br>
<br>
My proposal is to set the self-referential <tt>weak_ptr</tt> before
calling the constructor. This would allow <tt>shared_from_this()</tt>
to be used in the constructor.<br>
<br>
In pseudocode it would look something like this:<br>
<br>
<tt>namespace std</tt><tt><br>
</tt><tt>{</tt><tt><br>
</tt><tt>=C2=A0=C2=A0=C2=A0 template <typename T, typename... Params=
></tt><tt><br>
</tt><tt>=C2=A0=C2=A0=C2=A0
enable_if_t<is_base_of<enable_shared_from_this<T>, T
>::value,<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 shared_ptr<T> ></tt><tt><br>
</tt><tt>=C2=A0=C2=A0=C2=A0 make_shared(Params&&... parameters)=
<br>
=C2=A0=C2=A0=C2=A0 {</tt><tt><br>
</tt><tt>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 </tt><tt>ALLOCATE M=
EMORY<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 CREATE SHARED POINTER TO A=
DDRESS<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SET SELF-REFERENTIAL WEAK =
POINTER<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 T(parameters...);<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 RETURN SHARED POINTER<br>
=C2=A0=C2=A0=C2=A0 }<br>
</tt><tt>}</tt><br>
<br>
Any thoughts?<br>
<br>
<div class=3D"moz-signature">-- <br>
<b>Andrew Dunn</b></div>
</body>
</html>
<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 />
.