gpt4 book ai didi

c++ - 如何转发声明要在 unique_ptr 的标准容器中使用的类

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:03:15 26 4
gpt4 key购买 nike

在智能指针的标准容器中使用它时,是否可以避免让完整的类定义可见?例如,我无法编译以下内容:

#include <memory>
#include <map>
class Foo;
class Bar {
public:
Bar();
std::map<int, std::unique_ptr<Foo>> myMap;
};

Clang 编译器似乎坚持在编译 Bar 时提供完整的 Foo 定义。有没有一种技术可以用来避免必须包含 Foo.h?

编辑1:

error: invalid application of 'sizeof' to an incomplete type 'Foo':  static_assert(sizeof(_Tp) > 0, "default_delete can not delete incomplete type");

Edit2:不,它不是 is std::unique_ptr required to know the full definition 的拷贝.仅使用 unique_ptr 不一定需要完整定义,但在标准容器内使用它会引入额外的皱纹。

Edit3:事实证明,我可以(几乎)通过引入一个带有虚拟析构函数的基类来实现我想要的。然后,一个包含指向基类的智能指针容器的类将在没有问题的情况下进行编译。基类中不必存在任何其他内容,并且基类必须完全可见。这样,在编译容器时可以隐藏派生类的所有复杂性。

最佳答案

虽然严格的前向声明不会编译出智能指针的容器,但仍然可以解决大多数实际需求,避免在使用容器定义用户类时暴露完整的类。只需引入一个带有公共(public)虚拟干扰器的基类并使其可见。

在 fooBase.h 中

class FooBase {
public virtual ~FooBase();
};

在 bar.h 中

 #include "foobase.h"
#include <memory>
#include <map>
class Bar {
...
std::map<int, std::unique_ptr<FooBase>> myMap;
}

当 Foo 从 FooBase 派生并放在它自己的头文件中时,对 Foo 的更改将不再需要重新编译 Bar。

关于c++ - 如何转发声明要在 unique_ptr 的标准容器中使用的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25795363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com