为什么 make_unique 使用私有构造函数失败?

ark*_*974 5 c++ c++14

我正在实现一个单例类作为 c++14 中的练习,其中必须使用智能指针。此代码片段有效,但我想使用 make_unique ,不要与旧的 new 和 delete 关键字混淆,但它说它无法访问私有成员。为什么会发生?关键字“new”与智能指针一起使用是否安全?

#include <iostream>
#include <memory>

using namespace std;

class foo
{
    private:
        int val_;

    private:    
        foo() {}

    public:
        // properties
        void set(const int v) { val_ = v; }
        int  get() const { return val_; }

        //methods
        static const unique_ptr<foo>& get_instance()
        {
            static unique_ptr<foo> pfoo( new foo() );
            //unique_ptr<foo> pfoo = make_unique<foo>() ;  // Error - cannot access private member declared in class 'foo'
            return pfoo;        
        }


};

int main()
{
    foo::get_instance()->set(100);
    cout << foo::get_instance()->get() <<endl;
}
Run Code Online (Sandbox Code Playgroud)

编辑:get_instance() 声明为 const 以禁止调用者重置。