Boost logo

Boost Users :

Subject: Re: [Boost-users] Library Developers Battling Cyclic Header Dependency
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2011-03-16 14:18:38


>From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Ryan
>In the boost library system certain libraries need to be compiled and linked into a project.
>For example, libraries would need to be compiled to remove cyclic header dependencies.
>In the boost library system I coundn't find any .cpp files that would indecate a compilable library.
>So how do library developers create compilable files in the boost libraries?
>The following two files would need to be broken up and compiled to remove the dependencies, correct?
>
>//File A.hpp
>class B;
>
>class A {
> double x;
>public:
> B convertToB(void) {
> return B(x);
> }
>};
>
>//File B.hpp
>class A;
>
>class B {
> double y;
>public:
> A convertToA(void) {
> return A(y);
> }
>};

Not exactly, you have a syntax error because B() is not declared at the time you use it in A.hpp, assuming A.hpp is included first, otherwise it is A() that is not declared when used in B.hpp. I'm guessing this is the problem you are asking about? No boost library should have syntax errors of this type. If you rewrite it as follows it should compile as long as you include A.hpp first then B.hpp. You also need to declare the functions inline to prevent multiple definition link errors since the classes and their member functions are not templated.

//File A.hpp
class B;
 
class A {
  double x;
public:
  B inline convertToB(void);
};
 
//File B.hpp
class A;
 
class B {
  double y;
public:
  A inline convertToA(void) {
    return A(y);
  }
};

B A::convertToB(void) {
  return B(x);
}

This type of cyclical dependency can happen, and I have several such in my own library, but should not cause a problem in compilation and build in general. There are more rules about needing to define functions before using them and when you introduce templates you get still more rules about functions need to be defined at the time a template that uses them is instantiated, but boost library authors will know and follow all of these rules.

Regards,
Luke


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net