How do I call ::std::make_shared on a class with only protected or private constructors?

前端 未结 16 2840
遥遥无期
遥遥无期 2020-11-22 09:07

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

testmakeshared.cpp

#include 

class A {
 public:
   stat         


        
16条回答
  •  一个人的身影
    2020-11-22 09:47

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

    #include 
    #include 
    
    template
    struct enable_make : public S
    {
        template
        enable_make(T&&... t)
            : S(std::forward(t)...)
        {
        }
    };
    
    class foo
    {
    public:
        static std::unique_ptr create(std::unique_ptr u, char const* s)
        {
            return std::make_unique>(std::move(u), s);
        }
    protected:
        foo(std::unique_ptr u, char const* s)
        {
        }
    };
    
    void test()
    {
        auto fp = foo::create(std::make_unique(3), "asdf");
    }
    

提交回复
热议问题