c++ - boost::intrusive_ptr 类的前向声明以减少编译时间

标签 c++ boost forward-declaration

我有 A 类,它使用 boost::intrusive_ptr 保存一些数据:

#include "Data.h"

class A {
    boost::intrusive_ptr<Data> data;
}

Data 是基类 RefCounted 的后继类,根据需要为其实现函数 intrusive_ptr_releaseintrusive_ptr_add_ref .

我要减少编译时间,所以我尝试使用前向声明:

class Data;

class A {
    boost::intrusive_ptr<Data> data;
}

它不编译说

'intrusive_ptr_release': identifier not found

我尝试添加所需函数的声明:

class Data;

void intrusive_ptr_add_ref(RefCounted *);
void intrusive_ptr_release(RefCounted *);

class A {
    boost::intrusive_ptr<Data> data;
}

现在它说

'void intrusive_ptr_release(RefCounted *)': cannot convert argument 1 from 'Data *' to 'RefCounted *' pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

我理解编译器错误的含义:它不知道 RefCountedData 的父类(super class),因为 Data 是一个不完整的类型。但是,无论如何,这里有什么方法或技巧可以避免在处理 boost 侵入式指针时包含 header Data.h 以加快编译速度?

最佳答案

我知道解决您的问题的一种方法是确保您的头文件中没有(隐式)为 A 定义的构造函数或析构函数。最小的例子看起来像:

(头文件)

#include <boost/intrusive_ptr.hpp>
class Data;

struct A {
    boost::intrusive_ptr<Data> data;
    A();
    ~A();
};

void foo() {
   A a;
}

然后,您会在某处有一个 .cpp 文件,它会为 A 定义(可能默认)构造函数和析构函数,并包含类 Data 的定义。

关于c++ - boost::intrusive_ptr 类的前向声明以减少编译时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767676/

相关文章:

c++ - 在 FreeBSD 中实现 ioctl() 命令

c++ - 我怎么知道 boost::regex_replace 是否进行了更改?

c++ - 具有静态成员的类的前向声明

c - 将指针传递给 C 中的私有(private)结构?

c++ - 将任何函数作为模板参数传递

c++ - 如何检查字符数组中是否存在字符串值?

c++ - 以多态类型作为函数参数的 std::function 的容器

c++ - boost::program_options 是否支持要求一系列替代方案中的一个?

c++ - 从 boost ptime 获取年份

c - 如何在 C 中引用出现在需要它的函数之后的静态数据?