If my understanding is correct, the following is a classical circular dependency between template classes:
template <class MyB>
struct A {
MyB *b_;
};
template <class MyA>
struct B {
MyA *a_;
};
If we want to instantiate A
with B
and B
with A
, then we can't begin with either one, since we would have to write: A<B<A<B<...>>>
(infinite).
I think that template template parameters provide a solution. The following code compiles (with gcc
version 4.8.2):
template <class MyB>
struct A {
MyB *b_;
};
template <template <class> class MyA>
struct B {
MyA<B> *a_;
};
int main() {
using MyB = B<A>;
using MyA = A<MyB>;
MyA a;
MyB b;
a.b_ = &b; b.a_ = &a;
return 0;
}
Am I missing the essence of the problem?
UPDATE: I just ran into this post which proposes essentially the same solution.