你能把一个 pimpl-Class 放在一个 vector 里面吗

Can you put a pimpl-Class inside a vector

我有一个 class 使用 PImpl Ideom 实现的:

class FooImpl {};

class Foo
{
   unique_ptr<FooImpl> myImpl;
public:
   Foo();
   ~Foo();
};

现在我想把它放到 std::vector

void Bar()
{
   vector<Foo> testVec;
   testVec.resize(10);
}

但是当我这样做时,出现编译错误 (VC++ 2013)

error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function

我在 testVec.emplace_back();testVec.push_back(std::move(Foo()));

中遇到同样的错误

(作为解决方法,使用 vector<unique_ptr<Foo>> 似乎可行,但我不明白为什么上面的代码不起作用。)

工作示例:http://coliru.stacked-crooked.com/a/b274e1209e47c604

由于 std::unique_ptr 不可复制,class Foo 没有有效的复制构造函数。

您可以 deep copy or use a move constructor:

#include <memory>
#include <vector>

class FooImpl {};

class Foo
{
   std::unique_ptr<FooImpl> myImpl;
public:
   Foo( Foo&& f ) : myImpl( std::move( f.myImpl ) ) {}
   Foo(){}
   ~Foo(){}
};

int main() {
    std::vector<Foo> testVec;
    testVec.resize(10);
    return 0;
}

实例:https://ideone.com/HYtPMu

所以发生的是 vector 模板试图访问 Foo class 的复制构造函数。您没有提供一个,因此编译器会尝试生成一个默认实现,该实现会调用所有成员的复制构造函数。由于 std::unique_ptr 没有来自另一个 std::unique_ptr 的复制构造函数(这是合乎逻辑的,因为它不知道如何复制对象)编译器无法为 Foo 生成赋值运算符并且它失败。所以你可以做的是为 Foo class 提供一个复制构造函数并决定如何处理指针:

#include <memory>
#include <vector>

using namespace std;
class FooImpl {};

class Foo
{
    unique_ptr<FooImpl> myImpl;
public:
    Foo()
    {
    }
    ~Foo()
    {
    }
    Foo(const Foo& foo)
    {
        // What to do with the pointer?
    }
    Foo& operator= (const Foo& foo)
    {
        if (this != &foo)
        {
            // What to do with the pointer?
        }
        return *this;
    }
};

int main(int argc, char** argv)
{
    vector<Foo> testVec;
    testVec.resize(10);
    return 0;
}