I have two template classes which both depend on each other. As an example consider
template< typename > struct B;
template< typename T > struct A {
void doSomething() {
B<T> b{t};
b->doSomething();
// do some more
}
T t;
};
template< typename T > struct B {
void doSomething() {
// do something
}
A<T> createA() {
// Something
}
};
Based on my understanding of this, two-phase-lookup renders my snippet above ill-formed, because A
's implementation requires type B
to be complete, but at the time A
is implemented here, B
is still incomplete.
If these were not template classes, the problem would vanish as we would only require B
in A.cpp
. Therefore, when B.h
includes A.h
(because B
should actually require A
to be complete at the time it declares createA
).
However, since these are templates, we have to define everything in header files, which seems to makes this a lot more complicated.
How are such situations typically resolved?