如何解决在 std::vector 中存储使用 PIMPL 习惯用法的类
我正在编写一个应用程序,该应用程序需要在 std::vector
中存储使用 PIMPL 习语的类的对象。由于该类使用 std::unique_ptr
来存储指向其实现的指针,并且 std::unique_ptr
不可复制,因此该类本身不可复制。 std::vector
在这种情况下应该仍然有效,因为该类仍然可移动。
为了避免创建副本,我尝试使用 emplace_back
将元素直接构造到 vector
中,但由于某种原因它仍然抱怨它正在尝试调用复制构造函数!
我写了一个简单的例子来演示这个问题。
test.h:
#pragma once
#include <memory>
// Simple test class implemented using the PIMPL (pointer to implementation) idiom
class Test
{
public:
Test(const int value);
~test();
void DoSomething();
private:
// Forward declare implementation struct
struct Impl;
// Pointer to the implementation
std::unique_ptr<Impl> m_impl;
};
test.cpp
#include "test.h"
#include <iostream>
// Implementation struct deFinition
struct Test::Impl
{
Impl(const int value)
: m_value(value)
{}
void DoSomething()
{
std::cout << "value = " << m_value << std::endl;
}
private:
int m_value;
};
// Construct the class and create an instance of the implementation struct
Test::Test(const int value)
: m_impl(std::make_unique<Impl>(value))
{}
Test::~test() = default;
// Forward function calls to the implementation struct
void Test::DoSomething()
{
m_impl->DoSomething();
}
main.cpp:
#include "test.h"
#include <vector>
int main()
{
std::vector<Test> v;
// Even though I'm using "emplace_back" it still seems to be invoking the copy constructor!
v.emplace_back(42);
return 0;
}
error C2280: 'Test::Test(const Test &)': attempting to reference a deleted function
这就引出了两个问题...
解决方法
在您的 cpp 文件中添加 Test(Test&&) noexcept;
和 Test& operator=(Test&&) noexcept;
,然后 =default
。
您可能应该在使用时明确说明 Test(const int value)
。
至少在 modern gcc/clang 中,您可以=default
标题中的移动构造函数。您不能对 operator=
执行此操作,因为它可以删除左侧指针,也不能删除零参数构造函数(与构造默认删除器有关?)或析构函数(必须调用默认删除器)。>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。