246

I have this code that doesn't work, but I think the intent is clear:

testmakeshared.cpp

#include <memory>

class A {
 public:
   static ::std::shared_ptr<A> create() {
      return ::std::make_shared<A>();
   }

 protected:
   A() {}
   A(const A &) = delete;
   const A &operator =(const A &) = delete;
};

::std::shared_ptr<A> foo()
{
   return A::create();
}

But I get this error when I compile it:

g++ -std=c++0x -march=native -mtune=native -O3 -Wall testmakeshared.cpp
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:52:0,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/memory:86,
                 from testmakeshared.cpp:1:
testmakeshared.cpp: In constructor ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc) [with _Tp = A, _Alloc = std::allocator<A>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:518:8:   instantiated from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = A, _Alloc = std::allocator<A>, _Args = {}, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:986:35:   instantiated from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>, _Args = {}, _Tp = A, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:313:64:   instantiated from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<A>, _Args = {}, _Tp = A]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:531:39:   instantiated from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = A, _Alloc = std::allocator<A>, _Args = {}]’
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr.h:547:42:   instantiated from ‘std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = A, _Args = {}]’
testmakeshared.cpp:6:40:   instantiated from here
testmakeshared.cpp:10:8: error: ‘A::A()’ is protected
/usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/shared_ptr_base.h:400:2: error: within this context

Compilation exited abnormally with code 1 at Tue Nov 15 07:32:58

This message is basically saying that some random method way down in the template instantiation stack from ::std::make_shared can't access the constructor because it's protected.

But I really want to use both ::std::make_shared and prevent anybody from making an object of this class that isn't pointed at by a ::std::shared_ptr. Is there any way to accomplish this?

6
  • You can mark the function deep down that needs the constructor as friend, but that won't be portable.
    – Daniel
    Nov 16, 2011 at 5:25
  • @Dani: Yeah, it would be nice to have a portable solution. But that would work. Nov 16, 2011 at 5:40
  • 1
    Why not use return std::shared_ptr<A>(new A()), instead of return std::make_shared<A>() ?
    – Vassilis
    Sep 16, 2021 at 18:52
  • 1
    Because make_shared only does 1 allocation for the object and reference counts.
    – Trass3r
    Oct 27, 2021 at 22:27
  • @Omnifarious How do you think about this solution. Does it suit your need? If I miss something, please let me know.
    – John
    Jun 11, 2022 at 1:56

19 Answers 19

132

This answer is probably better, and the one I'll likely accept. But I also came up with a method that's uglier, but does still let everything still be inline and doesn't require a derived class:

#include <memory>
#include <string>

class A {
 protected:
   struct this_is_private;

 public:
   explicit A(const this_is_private &) {}
   A(const this_is_private &, ::std::string, int) {}

   template <typename... T>
   static ::std::shared_ptr<A> create(T &&...args) {
      return ::std::make_shared<A>(this_is_private{0},
                                   ::std::forward<T>(args)...);
   }

 protected:
   struct this_is_private {
       explicit this_is_private(int) {}
   };

   A(const A &) = delete;
   const A &operator =(const A &) = delete;
};

::std::shared_ptr<A> foo()
{
   return A::create();
}

::std::shared_ptr<A> bar()
{
   return A::create("George", 5);
}

::std::shared_ptr<A> errors()
{
   ::std::shared_ptr<A> retval;

   // Each of these assignments to retval properly generates errors.
   retval = A::create("George");
   retval = new A(A::this_is_private{0});
   return ::std::move(retval);
}

Edit 2017-01-06: I changed this to make it clear that this idea is clearly and simply extensible to constructors that take arguments because other people were providing answers along those lines and seemed confused about this.

19
  • 17
    Actually, I am a huge fan of those meaningless structures used only as keys. I prefer this to Luc's solution, but that might be my biais against inheritance. Nov 16, 2011 at 7:43
  • 2
    Agreed, I like this better as well.
    – ildjarn
    Nov 16, 2011 at 19:04
  • 3
    @Berkus: Then make it protected instead of private. And by "it", I'm referring to the this_is_private class, which maybe should be renamed in such case. I usually call it constructor_access in my code.
    – dalle
    Feb 16, 2014 at 14:02
  • 4
    Stefan, if you give this_is_private a private ctor you can make class A a friend. Seems to close the loophole. Jul 18, 2014 at 8:12
  • 2
    This question already has enough alternative answers (though many of them appear to be duplicate variations of the same themes). I am using the PassKey idiom in other areas, and I found that the two top-voted answers here actually combine quite nicely. I added a private make_shared_enabler class which derives from my main class, but the main class' constructor takes a PassKey for that make_shared_enabler class. This allows me to hide the constructor from the public API by making it protected, while disallowing (additional) subclassing thanks to the PassKey.
    – monkey0506
    Sep 13, 2019 at 3:03
97

Looking at the requirements for std::make_shared in 20.7.2.2.6 shared_ptr creation [util.smartptr.shared.create], paragraph 1:

Requires: The expression ::new (pv) T(std::forward<Args>(args)...), where pv has type void* and points to storage suitable to hold an object of type T, shall be well formed. A shall be an allocator (17.6.3.5). The copy constructor and destructor of A shall not throw exceptions.

Since the requirement is unconditionally specified in terms of that expression and things like scope aren't taken into account, I think tricks like friendship are right out.

A simple solution is to derive from A. This needn't require making A an interface or even a polymorphic type.

// interface in header
std::shared_ptr<A> make_a();

// implementation in source
namespace {

struct concrete_A: public A {};

} // namespace

std::shared_ptr<A>
make_a()
{
    return std::make_shared<concrete_A>();
}
14
  • 1
    Oh, that's a very clever answer, and possibly better than another one I had thought of. Nov 16, 2011 at 5:41
  • 1
    One question though, won't the shared_ptr delete an A and not a concrete_A, and couldn't this cause problems? Nov 16, 2011 at 5:51
  • 8
    Ahh, it's because shared_ptr stores a deleter at the time of instantiation, and if you're using make_shared the deleter absolutely has to be using the right type. Nov 16, 2011 at 6:23
  • 1
    @LucDanton Question is not about interfaces, as the title suggests he is also asking for a private ctor. Also, that's why I'm on this question anyway. Some old code having machiavelli classes, which has a private ctor and a create method returning a raw pointer, and I am trying to convert them to smart pointers.
    – zahir
    Aug 11, 2015 at 7:09
  • 3
    I like this approach (using it myself) but you do need a virtual destructor. It extends well to constructors with arguments (just provide a passthrough constructor). And if you are using protected rather than private you can make it completely invisible to users of the header.
    – Joe Steele
    Aug 28, 2017 at 18:43
94

Possibly the simplest solution. Based on the previous answer by Mohit Aron and incorporating dlf's suggestion.

#include <memory>

class A
{
public:
    static std::shared_ptr<A> create()
    {
        struct make_shared_enabler : public A {};

        return std::make_shared<make_shared_enabler>();
    }

private:
    A() {}  
};
5
  • 10
    if A has non-default constructors you will also need to expose them: struct make_shared_enabler : public A { template <typename... Args> make_shared_enabler(Args &&... args):A(std::forward<Args>(args)...) {} };. This makes all private constructors of A visible as make_shared_enabler constructors. Using constructors inheritance feature (using A::A;) seems doesn't help here because constructors will be still private.
    – anton_rh
    Dec 14, 2015 at 6:44
  • 3
    @anton_rh: you can't add template arguments to inner classes. See here.
    – bobbel
    Feb 17, 2016 at 20:20
  • 3
    Hm... Seems you are right. In my case struct was not local, but was a private struct: class A { ... private: struct A_shared_enabler; }; class A::A_shared_enabler : public A { ... }. See here cpp.sh/65qbr.
    – anton_rh
    Feb 18, 2016 at 11:31
  • This works great. Is there any chance of making this an inheritable property, so this pattern doesn't have to be repeated multiple times? Particularly the version that exposes non-default constructors would be very interesting to me. The default version would "merely" require some syntactic construct that replaces A with whatever the class is which inherits the class. I'm not aware of anything like that, but I wouldn't be surprised to learn that it exists... Jun 9, 2019 at 9:28
  • 1
    @KjeldSchmidt see my answer for a factorization of this pattern, at the cost of having protected ctors, not private ctors. Aug 4, 2022 at 13:28
46

Here's a neat solution for this:

#include <memory>

class A {
   public:
     static shared_ptr<A> Create();

   private:
     A() {}

     struct MakeSharedEnabler;   
 };

struct A::MakeSharedEnabler : public A {
    MakeSharedEnabler() : A() {
    }
};

shared_ptr<A> A::Create() {
    return make_shared<MakeSharedEnabler>();
}
6
  • 6
    I like this. It can be made a little simpler by defining MakeSharedEnabler locally inside A::Create().
    – dlf
    Jul 29, 2014 at 13:34
  • Awesome idea Mohit it helped me a lot.
    – Jnana
    Dec 17, 2016 at 10:36
  • @dlf No you can't do this for non-template class as in an example. A class will be incomplete. Feb 23, 2021 at 11:44
  • @dlf I did misread this, thought you mean defining MakeSharedEnabler in A. Thou you can't define MakeSharedEnabler inside Create either because it aint eclosing class A, you will get compile error. You should define MakeSharedEnabler before Create in order to make this work. Also you can't define Create inside class A as you will need MakeSharedEnabler definition to be able to convert MakeSharedEnabler into A, you do can return shared_ptr< MakeSharedEnabler> from Create, but this will be a bit uglier for client of the code and somewhate breaking encapsulation. Feb 23, 2021 at 12:12
  • For template <class T> class A; you do can define both MakeSharedEnabler and Create inside A. Feb 23, 2021 at 12:18
17

How about this?

static std::shared_ptr<A> create()
{
    std::shared_ptr<A> pA(new A());
    return pA;
}
6
  • 23
    That works great. But ::std::make_shared has functionality above and beyond simply making a shared_ptr to something. It allocates the reference count along with the object so they're located close to each other. I really, really want to use ::std::make_shared. Nov 16, 2011 at 5:23
  • The deleted assigment and copy operators forbid this
    – Daniel
    Nov 16, 2011 at 5:23
  • 10
    This is really the most straightforward approach, even though it isn't really what the question was asking. make_shared does have some nice characteristics and I try to use it wherever possible, but in this situation it seems quite likely that the run-time performance advantages of make_shared do not outweigh the extra code complexity and ceremony actually required to use it. If you really need the performance of make_shared then go crazy, but don't overlook the simplicity of just using shared_ptr's constructor.
    – Kevin
    Mar 19, 2014 at 17:18
  • 1
    Be careful about memory leaks though... see this question stackoverflow.com/a/14837300/2149539
    – dgmz
    Jun 21, 2018 at 11:52
  • 2
    @smac89 - en.cppreference.com/w/cpp/memory/shared_ptr/make_shared#Notes - See the notes section, the first bullet point. It's a suggested implementation in the standard. Jan 2, 2021 at 21:00
17
struct A {
public:
  template<typename ...Arg> std::shared_ptr<A> static create(Arg&&...arg) {
    struct EnableMakeShared : public A {
      EnableMakeShared(Arg&&...arg) :A(std::forward<Arg>(arg)...) {}
    };
    return std::make_shared<EnableMakeShared>(std::forward<Arg>(arg)...);
  }
  void dump() const {
    std::cout << a_ << std::endl;
  }
private:
  A(int a) : a_(a) {}
  A(int i, int j) : a_(i + j) {}
  A(std::string const& a) : a_(a.size()) {}
  int a_;
};
4
  • This is largely the same thing as Luc Danton's answer, although turning it into a local class is a nice touch. Some explanation to accompany the code could make this a much better answer.
    – user743382
    Jan 8, 2015 at 7:08
  • Normally, I want to write such small function in header file but not cc file. Second, in practice, I use a macro which looks like #define SharedPtrCreate(T) template<typename ...Arg>.....
    – alpha
    Jan 8, 2015 at 8:27
  • Good answer. I'd even put that into a macro called like IMPLEMENT_CREATE_SHARED(ClassName)
    – ivan.ukr
    Feb 6, 2020 at 8:15
  • One issue with this is that now it is a little bit more confusing what parameters create actually takes, and any errors will result in more confusing template messages. It's just one tradeoff from defining the helper class inside the actual create method. Apr 26, 2021 at 23:46
13

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_access with 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 <typename _T, typename ... _Args>
        static _T* __construct(void* __pv, _Args&& ... __args)
        { return ::new(__pv) _T(forward<_Args>(__args)...); }

        template <typename _T>
        static void __destroy(_T* __ptr) { __ptr->~_T(); }

        template <typename _T, typename _A>
        friend class __shared_ptr_storage;
    };
}

Usage

If/when the above is added to the standard, we would simply do:

class A {
public:
   static std::shared_ptr<A> create() {
      return std::make_shared<A>();
   }

 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.

0
9

Since I didn't like the already provided answers I decided to search on and found a solution that is not as generic as the previous answers but I like it better(tm). In retrospect it is not much nicer than the one provided by Omnifarius but there could be other people who like it too :)

This is not invented by me, but it is the idea of Jonathan Wakely (GCC developer).

Unfortunately it does not work with all the compilers because it relies on a small change in std::allocate_shared implementation. But this change is now a proposed update for the standard libraries, so it might get supported by all the compilers in the future. It works on GCC 4.7.

C++ standard Library Working Group change request is here: http://lwg.github.com/issues/lwg-active.html#2070

The GCC patch with an example usage is here: http://old.nabble.com/Re%3A--v3--Implement-pointer_traits-and-allocator_traits-p31723738.html

The solution works on the idea to use std::allocate_shared (instead of std::make_shared) with a custom allocator that is declared friend to the class with the private constructor.

The example from the OP would look like this:

#include <memory>

template<typename Private>
struct MyAlloc : std::allocator<Private>
{
    void construct(void* p) { ::new(p) Private(); }
};

class A {
    public:
        static ::std::shared_ptr<A> create() {
            return ::std::allocate_shared<A>(MyAlloc<A>());
        }

    protected:
        A() {}
        A(const A &) = delete;
        const A &operator =(const A &) = delete;

        friend struct MyAlloc<A>;
};

int main() {
    auto p = A::create();
    return 0;
}

A more complex example that is based on the utility I'm working on. With this I could not use Luc's solution. But the one by Omnifarius could be adapted. Not that while in the previous example everybody can create an A object using the MyAlloc in this one there is not way to create A or B besides the create() method.

#include <memory>

template<typename T>
class safe_enable_shared_from_this : public std::enable_shared_from_this<T>
{
    public:
    template<typename... _Args>
        static ::std::shared_ptr<T> create(_Args&&... p_args) {
            return ::std::allocate_shared<T>(Alloc(), std::forward<_Args>(p_args)...);
        }

    protected:
    struct Alloc : std::allocator<T>
    {  
        template<typename _Up, typename... _Args>
        void construct(_Up* __p, _Args&&... __args)
        { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    };
    safe_enable_shared_from_this(const safe_enable_shared_from_this&) = delete;
    safe_enable_shared_from_this& operator=(const safe_enable_shared_from_this&) = delete;
};

class A : public safe_enable_shared_from_this<A> {
    private:
        A() {}
        friend struct safe_enable_shared_from_this<A>::Alloc;
};

class B : public safe_enable_shared_from_this<B> {
    private:
        B(int v) {}
        friend struct safe_enable_shared_from_this<B>::Alloc;
};

int main() {
    auto a = A::create();
    auto b = B::create(5);
    return 0;
}
0
9

I had the same problem, but none of the existing answers were really satisfactory as I need to pass arguments to the protected constructor. Moreover, I need to do this for several classes, each taking different arguments.

To that effect, and building on several of the existing answers which all use similar methods, I present this little nugget:

template < typename Object, typename... Args >
inline std::shared_ptr< Object >
protected_make_shared( Args&&... args )
{
  struct helper : public Object
  {
    helper( Args&&... args )
      : Object{ std::forward< Args >( args )... }
    {}
  };

  return std::make_shared< helper >( std::forward< Args >( args )... );
}
2
  • 2
    This is very cool, because it doesn't clutter the class. Thanks. If I make this protected_make_shared a public function in the global namespace, I had to add it as friend to the class like so: template<typename Object, typename... Args> friend shared_ptr<Object> protected_make_shared(Args&&... args);. But if this is only used by one class, you may add it as a (public) class member as static inline.
    – some guy
    Mar 25, 2022 at 8:57
  • 2
    The downside of this solution is that protected_make_shared can be called from anywhere, including from a class which is not friend with Object. Aug 4, 2022 at 10:29
5

There's a more hairy and interesting problem that happens when you have two strictly related classes A and B that work together.

Say A is the "master class" and B its "slave". If you want to restrict instantiation of B only to A, you'd make B's constructor private, and friend B to A like this

class B
{
public:
    // B your methods...

private:
    B();
    friend class A;
};

Unfortunately calling std::make_shared<B>() from a method of A will make the compiler complain about B::B() being private.

My solution to this is to create a public Pass dummy class (just like nullptr_t) inside B that has private constructor and is friend with A and make B's constructor public and add Pass to its arguments, like this.

class B
{
public:
  class Pass
  {
    Pass() {}
    friend class A;
  };

  B(Pass, int someArgument)
  {
  }
};

class A
{
public:
  A()
  {
    // This is valid
    auto ptr = std::make_shared<B>(B::Pass(), 42);
  }
};

class C
{
public:
  C()
  {
    // This is not
    auto ptr = std::make_shared<B>(B::Pass(), 42);
  }
};
4

I realise this thread is rather old, but I found an answer that does not require inheritance or extra arguments to the constructor that I couldn't see elsewhere. It is not portable though:

#include <memory>

#if defined(__cplusplus) && __cplusplus >= 201103L
#define ALLOW_MAKE_SHARED(x) friend void __gnu_cxx::new_allocator<test>::construct<test>(test*);
#elif defined(_WIN32) || defined(WIN32)
#if defined(_MSC_VER) && _MSC_VER >= 1800
#define ALLOW_MAKE_SHARED(x) friend class std::_Ref_count_obj;
#else
#error msc version does not suport c++11
#endif
#else
#error implement for platform
#endif

class test {
    test() {}
    ALLOW_MAKE_SHARED(test);
public:
    static std::shared_ptr<test> create() { return std::make_shared<test>(); }

};
int main() {
    std::shared_ptr<test> t(test::create());
}

I have tested on windows and linux, it may need tweaking for different platforms.

3
  • 1
    I'm tempted to -1 it for lack of portability. The other answers (particularly the 'key class' answers) are fairly elegant and the non-portable answer very ugly. I can't think of a reason you'd use the non-portable answer. It's not faster or anything like that. Dec 24, 2016 at 6:38
  • @Omnifarious It is indeed non-portable and I wouldn't recommend, but I believe this is in fact the semantically most correct solution. In my answer, I link to a proposal of adding std::shared_ptr_access to the standard, which could be seen as allowing to do the above in a simple and portable way. Jul 10, 2018 at 13:20
  • You can do something similar by friending std::construct_at instead nowadays. However both these approaches leak in that clients have public access to std::construct_at and __gnu_cxx::new_allocator May 2 at 14:39
4

If you also want to enable a constuctor that takes arguments, this may help a bit.

#include <memory>
#include <utility>

template<typename S>
struct enable_make : public S
{
    template<typename... T>
    enable_make(T&&... t)
        : S(std::forward<T>(t)...)
    {
    }
};

class foo
{
public:
    static std::unique_ptr<foo> create(std::unique_ptr<int> u, char const* s)
    {
        return std::make_unique<enable_make<foo>>(std::move(u), s);
    }
protected:
    foo(std::unique_ptr<int> u, char const* s)
    {
    }
};

void test()
{
    auto fp = foo::create(std::make_unique<int>(3), "asdf");
}
4

CRTP based solution that allows factorization for multiple classes, is easily enabled and works for constructors with arguments. It requires the constructor to be protected (not private). Usage is a bit similar to enable_shared_from_this. It does not have the downside of breaking the protected keyword i.e. classes that use ::make_unique will have to be friend. Inspired by the answer from Mark Tolley.

Implementation:

template <typename ClassWithProtectedCtor>
class enable_protected_make_unique
{
protected: // important, if public then equivalent to having the constructor public which is what we want to avoid!
    template <typename... Args>
    static std::unique_ptr<ClassWithProtectedCtor> make_unique(Args &&... args)
    {
        class make_unique_enabler : public ClassWithProtectedCtor
        {
        public:
            // it's from this line that comes the need to have the constructor protected, not private:
            make_unique_enabler(Args &&... args) : ClassWithProtectedCtor(std::forward<Args>(args)...) {}
        };
        return std::make_unique<make_unique_enabler>(std::forward<Args>(args)...);
    }
};

Example of usage:

class Factory;

class MyClassWithProtectedCtor : public enable_protected_make_unique<MyClassWithProtectedCtor>
{
friend Factory;
private:
    MyClassWithProtectedCtor(int a, double c) {};
}

class Factory
{
    std::unique_ptr<MyClassWithProtectedCtor> CreateMyClassWithProtectedCtor(int a, double c)
    {
        return MyClassWithProtectedCtor::make_unique(a, c);
    }
}

You can replace unique by shared, or combine both in the same "enabler" class.

Disclaimer: I have not tested that in production code, there may be downsides (such as longer error messages when the type MyClassWithProtectedCtor is mentioned).

2
  • 2
    You can make this solution more foolproof by adding static_assert(!std::is_constructible_v<ClassWithProtectedCtor, Args...>); to make_unique_enabler constructor. It will fail if the ClassWithProtectedCtor has its ctor public. Aug 16, 2022 at 9:57
  • A downside of this approach is that it prevents a class from being declared final. Aug 1 at 13:00
3

[Edit] I read through the thread noted above on a standardized std::shared_ptr_access<> proposal. Within there was a response noting a fix to std::allocate_shared<> and an example of its use. I've adapted it to a factory template below, and tested it under gcc C++11/14/17. It works with std::enable_shared_from_this<> as well, so would obviously be preferable to my original solution in this answer. Here it is...

#include <iostream>
#include <memory>

class Factory final {
public:
    template<typename T, typename... A>
    static std::shared_ptr<T> make_shared(A&&... args) {
        return std::allocate_shared<T>(Alloc<T>(), std::forward<A>(args)...);
    }
private:
    template<typename T>
    struct Alloc : std::allocator<T> {
        template<typename U, typename... A>
        void construct(U* ptr, A&&... args) {
            new(ptr) U(std::forward<A>(args)...);
        }
        template<typename U>
        void destroy(U* ptr) {
            ptr->~U();
        }
    };  
};

class X final : public std::enable_shared_from_this<X> {
    friend class Factory;
private:
    X()      { std::cout << "X() addr=" << this << "\n"; }
    X(int i) { std::cout << "X(int) addr=" << this << " i=" << i << "\n"; }
    ~X()     { std::cout << "~X()\n"; }
};

int main() {
    auto p1 = Factory::make_shared<X>(42);
    auto p2 = p1->shared_from_this();
    std::cout << "p1=" << p1 << "\n"
              << "p2=" << p2 << "\n"
              << "count=" << p1.use_count() << "\n";
}

[Orig] I found a solution using the shared pointer aliasing constructor. It allows both the ctor and dtor to be private, as well as use of the final specifier.

#include <iostream>
#include <memory>

class Factory final {
public:
    template<typename T, typename... A>
    static std::shared_ptr<T> make_shared(A&&... args) {
        auto ptr = std::make_shared<Type<T>>(std::forward<A>(args)...);
        return std::shared_ptr<T>(ptr, &ptr->type);
    }
private:
    template<typename T>
    struct Type final {
        template<typename... A>
        Type(A&&... args) : type(std::forward<A>(args)...) { std::cout << "Type(...) addr=" << this << "\n"; }
        ~Type() { std::cout << "~Type()\n"; }
        T type;
    };
};

class X final {
    friend struct Factory::Type<X>;  // factory access
private:
    X()      { std::cout << "X() addr=" << this << "\n"; }
    X(int i) { std::cout << "X(...) addr=" << this << " i=" << i << "\n"; }
    ~X()     { std::cout << "~X()\n"; }
};

int main() {
    auto ptr1 = Factory::make_shared<X>();
    auto ptr2 = Factory::make_shared<X>(42);
}

Note that the approach above doesn't play well with std::enable_shared_from_this<> because the initial std::shared_ptr<> is to the wrapper and not the type itself. We can address this with an equivalent class that is compatible with the factory...

#include <iostream>
#include <memory>

template<typename T>
class EnableShared {
    friend class Factory;  // factory access
public:
    std::shared_ptr<T> shared_from_this() { return weak.lock(); }
protected:
    EnableShared() = default;
    virtual ~EnableShared() = default;
    EnableShared<T>& operator=(const EnableShared<T>&) { return *this; }  // no slicing
private:
    std::weak_ptr<T> weak;
};

class Factory final {
public:
    template<typename T, typename... A>
    static std::shared_ptr<T> make_shared(A&&... args) {
        auto ptr = std::make_shared<Type<T>>(std::forward<A>(args)...);
        auto alt = std::shared_ptr<T>(ptr, &ptr->type);
        assign(std::is_base_of<EnableShared<T>, T>(), alt);
        return alt;
    }
private:
    template<typename T>
    struct Type final {
        template<typename... A>
        Type(A&&... args) : type(std::forward<A>(args)...) { std::cout << "Type(...) addr=" << this << "\n"; }
        ~Type() { std::cout << "~Type()\n"; }
        T type;
    };
    template<typename T>
    static void assign(std::true_type, const std::shared_ptr<T>& ptr) {
        ptr->weak = ptr;
    }
    template<typename T>
    static void assign(std::false_type, const std::shared_ptr<T>&) {}
};

class X final : public EnableShared<X> {
    friend struct Factory::Type<X>;  // factory access
private:
    X()      { std::cout << "X() addr=" << this << "\n"; }
    X(int i) { std::cout << "X(...) addr=" << this << " i=" << i << "\n"; }
    ~X()     { std::cout << "~X()\n"; }
};

int main() {
    auto ptr1 = Factory::make_shared<X>();
    auto ptr2 = ptr1->shared_from_this();
    std::cout << "ptr1=" << ptr1.get() << "\nptr2=" << ptr2.get() << "\n";
}

Lastly, somebody said clang complained about Factory::Type being private when used as a friend, so just make it public if that's the case. Exposing it does no harm.

1

The root of the problem is that if the function or class you friend makes lower level calls to your constructor, they have to be friended too. std::make_shared isn't the function that's actually calling your constructor so friending it makes no difference.

class A;
typedef std::shared_ptr<A> APtr;
class A
{
    template<class T>
    friend class std::_Ref_count_obj;
public:
    APtr create()
    {
        return std::make_shared<A>();
    }
private:
    A()
    {}
};

std::_Ref_count_obj is actually calling your constructor, so it needs to be a friend. Since that's a bit obscure, I use a macro

#define SHARED_PTR_DECL(T) \
class T; \
typedef std::shared_ptr<T> ##T##Ptr;

#define FRIEND_STD_MAKE_SHARED \
template<class T> \
friend class std::_Ref_count_obj;

Then your class declaration looks fairly simple. You can make a single macro for declaring the ptr and the class if you prefer.

SHARED_PTR_DECL(B);
class B
{
    FRIEND_STD_MAKE_SHARED
public:
    BPtr create()
    {
        return std::make_shared<B>();
    }
private:
    B()
    {}
};

This is actually an important issue. To make maintainable, portable code you need to hide as much of the implementation as possible.

typedef std::shared_ptr<A> APtr;

hides how you're handling your smart pointer a bit, you have to be sure to use your typedef. But if you always have to create one using make_shared, it defeats the purpose.

The above example forces code using your class to use your smart pointer constructor, which means that if you switch to a new flavor of smart pointer, you change your class declaration and you have a decent chance of being finished. DO NOT assume your next boss or project will use stl, boost etc. plan for changing it someday.

Doing this for almost 30 years, I've paid a big price in time, pain and side effects to repair this when it was done wrong years ago.

1
  • 3
    std::_Ref_count_obj is an implementation detail. That means while this solution might work for you, for now, on your platform. But it might not work for others and might stop working any time your compiler updates or maybe even if you just change compilation flags. Dec 27, 2018 at 20:32
0

If possible you could create a public move constructor like so:

class A {
 public:
   A(A&&) = default;
   static ::std::shared_ptr<A> create() {
      return ::std::make_shared<A>(std::move<A>(A{}));
   }

 protected:
   A() {}
   A(const A &) = delete;
   const A &operator =(const A &) = delete;
};

::std::shared_ptr<A> foo()
{
   return A::create();
}
1
  • This only works, because the move constructor is public and the type is movable, which may be undesirable or impossible. Aug 1 at 12:55
-1

How about this solution, it's simple and it also could achieve the goal. Here is the code snippet:

#include <iostream>
#include <memory>
 
class Foo : public std::enable_shared_from_this<Foo> {
private:     //the user should not construct an instance through the constructor below.                    
    Foo(int num):num_(num) { std::cout << "Foo::Foo\n"; }
public:
    Foo(const Foo&) = delete;
    Foo(Foo&&) = default;
    Foo& operator=(const Foo&) = delete;
    Foo& operator=(Foo&&) = default;

public:
    ~Foo() { std::cout << "Foo::~Foo\n"; } 

    int DoSth(){std::cout << "hello world" << std::endl; return 0;}

    std::shared_ptr<Foo> getPtr() { return shared_from_this();}

    static std::shared_ptr<Foo> Create() {
        Foo* foo = new Foo(5);
        return std::shared_ptr<Foo>(foo);
    }

private:
    int num_;

};

int main()
{
    auto sp = Foo::Create();
    sp->DoSth();

    Foo& foo = *sp.get();
    auto sp1 = foo.getPtr();

    std::cout << sp.use_count() << std::endl;
}
2
  • 1
    The downside of this solution is that it amounts to making the constructor public: non-friend classes can create new instances via Create(), so why not simply make the constructor public in this case ? An alternative solution that does not have this problem. Aug 10, 2022 at 7:33
  • 1
    There is a reason I want to use make_shared rather than shared_from_this. It avoids an allocation for the reference count. Additionally, the reference count ends up being stored near the referenced object. Aug 27, 2022 at 16:41
-1
class A  {
public:
 static std::shared_ptr<A> getA() {
    return std::shared_ptr<A> a(new A());
 }

private:
  A() {}
};

Since std::make_shared cannot call the private constructor we are making an instance of A manually with new. We then set the shared_ptr to point to the new A object in its constructor. You dont have to worry about leaking memory the shared_ptr will delete A for you.

4
  • Whilst this might provide a good answer, it's encouraged to leave some description about why it answers the question. Thank you.
    – user438383
    Sep 21, 2021 at 8:07
  • 1
    This doesn't use make_shared and so isn't an answer. There is a specific reason to use make_shared because it only does one allocation for both the object and the reference count. It also localizes access to the reference count to memory next to where the memory for the object is, improving processor caching and making it much more likely that the reference and the object are on the same VM page. Oct 30, 2021 at 17:56
  • This method only does one allocation for both the object and the reference count. Shared pointers store things on the heap much like new does. I think you are making wrong making assumptions. There is no reason why the memory location using new is less efficient then using make_shared
    – Max Gómez
    Nov 8, 2021 at 17:30
  • @Omnifarious is correct. The expression new A() cannot know that the object that is being allocated on the heap will be used with shared_ptr, so it cannot allocate memory for the reference counter, mutex and other parts of the implementation of shared_ptr. It only allocates an A on the heap, returns a raw pointer, and the shared_ptr constructor then must make another allocation on the heap for the reference counter. Aug 1 at 12:52
-3
#include <iostream>
#include <memory>

class A : public std::enable_shared_from_this<A>
{
private:
    A(){}
    explicit A(int a):m_a(a){}
public:
    template <typename... Args>
    static std::shared_ptr<A> create(Args &&... args)
    {
        class make_shared_enabler : public A
        {
        public:
            make_shared_enabler(Args &&... args):A(std::forward<Args>(args)...){}
        };
        return std::make_shared<make_shared_enabler>(std::forward<Args>(args)...);
    }

    int val() const
    {
        return m_a;
    }
private:
    int m_a=0;
};

int main(int, char **)
{
    std::shared_ptr<A> a0=A::create();
    std::shared_ptr<A> a1=A::create(10);
    std::cout << a0->val() << " " << a1->val() << std::endl;
    return 0;
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.