Blindy
Blindy

Reputation: 67352

C++ Modules with complex (but valid) cyclical dependencies

Just to forestall and confusion, what I'm talking about is valid and trivially done with header files. However when I tried using modules, I can't wrap my head around a fix, and the SO examples didn't quite answer my specific use case.

If all I need is a pointer to the class in module B that imports module A, and module A imports module B and uses it fully, that would be trivially handled with a forward class declaration in B:

export class A;
export class B
{
    shared_ptr<A> a; // or pointer or whatever is needed
};

However, my specific need is more complex: I need a static portion of A in my B class, specifically (but not relevantly) a using type definition:

// A.ixx
import B;

export class A
{
    using T = function<const X&>; // depends on B, simplified

    void fn(const B&); // depends on B, simplified 
};

// B.ixx
export class B
{
    vector<A::T> v; // depends on A::T, a static type defined in A
};

As seen above, forward declaring A won't fix my issue. I could re-define A::T, but that's silly -- I might as well go back to header files at that point.

So how does this kind of pattern get solved idiomatically?

Upvotes: 0

Views: 71

Answers (0)

Related Questions