无法访问私有成员 - template和std :: unique_ptr

Nic*_*ick 2 c++ templates unique-ptr c++11

我有以下代码:

#include <memory>

template<typename T, size_t Level>
class Foo
{
    friend class Foo<T, Level + 1>;
    typedef std::unique_ptr<T> ElmPtr;
    typedef std::unique_ptr<Foo<ElmPtr, Level - 1>> NodePtr;        

    public:
    Foo() {
        // no errors
        auto c = children;
    }

    Foo(int n) {
        // !!! compiler error !!!
        auto c = children;
    }

    std::array<NodePtr, 4> children;            
};

template<typename T>
class Foo<T, 0>
{
    friend class Foo<T, 1>;

    public:
    Foo() {}
};

int main()
{
    Foo<int, 1> foo1;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误C2248:'std :: unique_ptr <_Ty> :: unique_ptr':无法访问类'std :: unique_ptr <_Ty>'中声明的私有成员

为什么?我该如何解决这个问题?

Bar*_*rry 5

你有:

auto c = children;
Run Code Online (Sandbox Code Playgroud)

哪里:

std::array<std::unique_ptr<T>, N> children;            
Run Code Online (Sandbox Code Playgroud)

这将需要复制unique_ptrs,并且unique_ptr不可复制.你可以参考一下children:

auto& c = children; // OK 
Run Code Online (Sandbox Code Playgroud)