[llvm-bugs] [Bug 32979] New: private std::enable_shared_from_this prevents std::make_shared with C++1z
via llvm-bugs
llvm-bugs at lists.llvm.org
Tue May 9 08:51:11 PDT 2017
https://bugs.llvm.org/show_bug.cgi?id=32979
Bug ID: 32979
Summary: private std::enable_shared_from_this prevents
std::make_shared with C++1z
Product: libc++
Version: 4.0
Hardware: PC
OS: Windows NT
Status: NEW
Severity: normal
Priority: P
Component: All Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: p_hampson at wargaming.net
CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
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 http://wg21.link/p0033r1
#2
https://github.com/cplusplus/draft/commit/4013f7279a3b6a2b39658bac034ac06b10db4fbd
#3 [util.smartptr.shared.const]/1
http://eel.is/c++draft/util.smartptr.shared.const
#4 https://wandbox.org/permlink/syLLIeqUxWprhUod
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170509/b2aceb3c/attachment.html>
More information about the llvm-bugs
mailing list