Header-Only circular dependency

723 views Asked by At

I am having problems with a circular dependency in a head-only library for C++ that would have not been a circular dependency problem when using source files instead of making everything header-only.

The situation is equal to this:

There are four files for two classes A and B. Every class has got its header file (e.g. "A.hpp") and its implementation file (e.g. "A.tpp").

The dependencies are as follows:

  • A's header requires B' header
  • A's impl requires B's header
  • B's header requires nothing but a forward declaration to A
  • B's impl requires A's header

So in a source-based library everything would be fine with the following order of source file inclusion and compilation:

  • B.hpp
  • A.hpp
  • A.tpp or B.tpp (order here is not important)

My files are structured in this way:

File content for A.hpp:

#ifndef A_H
#define A_H
#include "B.hpp"
class A { ... };
#include "A.tpp"
#endif

File content for A.tpp:

#ifndef A_H
    #error "Do not include this file directly."
#endif
// ... some implementations for A.hpp

File content for B.hpp:

#ifndef B_H
#define B_H
class A;
class B { ... };
#include "B.tpp"
#endif

File content for B.tpp:

#include "A.hpp"
#ifndef B_H
    #error "Do not include this file directly."
#endif
// ... some implementations for B.hpp

So my question is: Is there a solution for breaking this unnessesary circular dependency which only happens due to the fact that I am using a header-only solution for my library?

0

There are 0 answers