简体   繁体   中英

Forward declaration for boost::intrusive_ptr class to reduce compile time

I have class A which holds some data using boost::intrusive_ptr :

#include "Data.h"

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

Class Data is a succesor of base class RefCounted for which functions intrusive_ptr_release and intrusive_ptr_add_ref are implemented as required.

I am going to reduce compile time, so I am trying to use forward declaration:

class Data;

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

It does not compile saying

'intrusive_ptr_release': identifier not found

I tried to add declarations of needed functions:

class Data;

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

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

Now it says

'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

I understand meaning of compiler errors: it does not know that RefCounted is a superclass for Data because Data is an incomplete type. But, anyway, is there any way or trick possible here to avoid including of header Data.h to speed up compilation when dealing with boost intrusive pointers?

The one solution to your problem I know is to make sure there are no (implicitly) defined constructors or destructors for A in your header file. The minimal example would look like:

(header file)

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

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

void foo() {
   A a;
}

Than, you'd have a .cpp file somewhere, which would define (could default) constructor and destructor for A, and include definition for class Data .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM