I have this code that doesn\'t work, but I think the intent is clear:
testmakeshared.cpp
#include
class A {
public:
stat
Ideally, I think the perfect solution would require additions to the C++ standard. Andrew Schepler proposes the following:
(Go here for the whole thread)
we can borrow an idea from boost::iterator_core_access. I propose a new class
std::shared_ptr_accesswith no public or protected members, and to specify that for std::make_shared(args...) and std::alloc_shared(a, args...), the expressions ::new(pv) T(forward(args)...) and ptr->~T() must be well-formed in the context of std::shared_ptr_access.An implementation of std::shared_ptr_access might look like:
namespace std {
class shared_ptr_access
{
template
static _T* __construct(void* __pv, _Args&& ... __args)
{ return ::new(__pv) _T(forward<_Args>(__args)...); }
template
static void __destroy(_T* __ptr) { __ptr->~_T(); }
template
friend class __shared_ptr_storage;
};
}
If/when the above is added to the standard, we would simply do:
class A {
public:
static std::shared_ptr create() {
return std::make_shared();
}
protected:
friend class std::shared_ptr_access;
A() {}
A(const A &) = delete;
const A &operator =(const A &) = delete;
};
If this also sounds like an important addition to the standard to you, feel free to add your 2 cents to the linked isocpp Google Group.