#pragma once #include namespace ext { /** * Class AllocateShared allow to make std::shared_ptr from T with private constructor. * Derive you T class from shared_ptr_helper, define him as friend and call allocate_shared()/make_shared() method. **/ template class shared_ptr_helper { protected: typedef typename std::remove_const::type TNoConst; template struct Deleter { void operator()(typename TAlloc::value_type * ptr) { std::allocator_traits::destroy(alloc, ptr); } TAlloc alloc; }; ///see std::allocate_shared template static std::shared_ptr allocate_shared(const TAlloc & alloc, TArgs && ... args) { TAlloc alloc_copy(alloc); return std::shared_ptr(new (std::allocator_traits::allocate(alloc_copy, 1)) TNoConst(std::forward(args)...), Deleter(), alloc_copy); } template static std::shared_ptr make_shared(TArgs && ... args) { return allocate_shared(std::allocator(), std::forward(args)...); } }; }