<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - private std::enable_shared_from_this prevents std::make_shared with C++1z"
href="https://bugs.llvm.org/show_bug.cgi?id=32979">32979</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>private std::enable_shared_from_this prevents std::make_shared with C++1z
</td>
</tr>
<tr>
<th>Product</th>
<td>libc++
</td>
</tr>
<tr>
<th>Version</th>
<td>4.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Windows NT
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>All Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>p_hampson@wargaming.net
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
</td>
</tr></table>
<p>
<div>
<pre>As of C++17 (P0033R1 (#1) merged in March 2016 (#2)), `std::make_shared` (and
the shared_ptr constructors that 'enable shared_from_this') should only attempt
to use an "unambiguous and accessible" (#3) std::enable_shared_from_this.
Although this produces the odd behaviour that a private
std::enable_shared_from_this will not be initialised by std::make_shared, it is
certainly expected to compile.
The minimal code-sample that demonstrates this is below (#4)
#include <memory>
#include <iostream>
#include <cassert>
struct A : private std::enable_shared_from_this<A> {
std::weak_ptr<A> get_weak() { return weak_from_this(); }
};
using APtr = std::shared_ptr<A>;
using AWPtr = std::weak_ptr<A>;
int main()
{
APtr pA1 = std::make_shared<A>();
AWPtr pWA2 = pA1->get_weak();
APtr pA2 = pWA2.lock();
std::cout << pA1.use_count() << "\n";
std::cout << "shared_from_this " << (pWA2.expired() ? "not enabled" :
"enabled") << "\n";
assert( pA1.use_count() == 1 );
assert( pWA2.expired() );
}
The assertions pass as-of gcc 7.
#1 <a href="http://wg21.link/p0033r1">http://wg21.link/p0033r1</a>
#2
<a href="https://github.com/cplusplus/draft/commit/4013f7279a3b6a2b39658bac034ac06b10db4fbd">https://github.com/cplusplus/draft/commit/4013f7279a3b6a2b39658bac034ac06b10db4fbd</a>
#3 [util.smartptr.shared.const]/1
<a href="http://eel.is/c++draft/util.smartptr.shared.const">http://eel.is/c++draft/util.smartptr.shared.const</a>
#4 <a href="https://wandbox.org/permlink/syLLIeqUxWprhUod">https://wandbox.org/permlink/syLLIeqUxWprhUod</a></pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>