Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings Kind Regards May I please inquire do C++ modules solve the problem of circular dependency Also how in the world can a templated class or function be contained in a module since its' template type parameters are not know until called from the importing file Thank You Kindly - Cheerio

What I have tried:

Searched web for discussion and documentation to no avail
Posted
Updated 14-Aug-21 2:02am

1. Circular dependency is usually the reult of bad design or coding.
2. Full details of templates and their implementation can be found at cppreference.com[^] and C++ Language Reference | Microsoft Docs[^].


[edit]
Overview of modules in C++ | Microsoft Docs[^] gives a clear explanation of modules and their implementation. So, C++ is finally following the Java way of doing things. :))
[/edit]
 
Share this answer
 
v2
Comments
BernardIE5317 14-Aug-21 4:27am    
Thank You I am reasonably familiar w/ templates I utilize them almost universally for my classes and functions Circular dependency is a problem I have yet to solve I was hoping modules would do so for me The only method I know of to compile w/ circular dependency is to present forward references utilizing pointers or references to member classes and implementation details for each header file following all the forward references I will however attempt to heed your advice and attempt to redesign my code to avoid the problem in the first place Thank You Kindly - Cheerio
Richard MacCutchan 14-Aug-21 4:50am    
I do not understand what you mean by "Circular dependency is a problem I have yet to solve I was hoping modules would do so for me". What do you mean here by "module", and in what way are they related to circular dependency?

At its simplest a circular dependency is something like:
class B;
class A
{
private:
    B b;
};
class B
{
private:
    A a;
};
BernardIE5317 14-Aug-21 5:16am    
By "modules" I mean The new C++ feature as per https://eel.is/c%2B%2Bdraft/module As for their relation to circular dependency I do not know I was hoping for some module magic Unless I can avoid circular dependency in my code my solution will be

#include <memory>
#include <iostream>
using namespace std;

#define FUNCSIG cout << __FUNCSIG__ << endl;

class A;
class B;
class A
{
private:
unique_ptr b;
public:
A();
void set_B(unique_ptr&& _b);
void do_something_with_B();
void foobar_A();
};

class B
{
private:
unique_ptr a;
public:
B();
void set_A(unique_ptr
&& _a);
void do_something_with_A();
void foobar_B();
};

A::A() : b(nullptr) { FUNCSIG }
void A::set_B(unique_ptr&& _b) { b = move(_b); }
void A::do_something_with_B() { FUNCSIG b->foobar_B(); }
void A::foobar_A() { FUNCSIG }

B::B() : a(nullptr) { FUNCSIG }
void B::set_A(unique_ptr
&& _a) { a = move(_a); }
void B::do_something_with_A() { FUNCSIG a->foobar_A(); }
void B::foobar_B() { FUNCSIG }

int main()
{
cout << "Hello World\n";
A a;
a.set_B(make_unique());
B b;
b.set_A(make_unique
());
a.do_something_with_B();
b.do_something_with_A();
}
Richard MacCutchan 14-Aug-21 5:19am    
Generally speaking if you need to do that then your design needs a rethink.
Richard MacCutchan 14-Aug-21 5:21am    
I have just been looking at Overview of modules in C++ | Microsoft Docs[^] which gives a much clearer explanation of what a module is. So, C++ is finally following the Java way of doing things.
When you use a class in name only, you don't need to #include the header that defines it. Instead, you can just declare it as a forward reference:
C++
class SomeClass;
A header that contains this declaration can now declare functions with a parameter or result whose type is a pointer or a reference to SomeClass. This is a common approach for resolving what would otherwise be a circular dependency.

In name only means that the compiler doesn't need to know the size of SomeClass. It is only being accessed through a pointer or reference, and the size of the pointer or reference is known to be either 32 or 64 bits.

The .cpp that implements one of these functions will have to #include the header that defines SomeClass, but this won't cause a circular dependency because all .cpps are leaves in the dependency graph.
 
Share this answer
 
v2
For some reason the type arguments to unique_ptr and make_unique are not displayed in all instances at least in the preview so read w/ caution

C++
#include <memory>
#include <iostream>
using namespace std;

#define FUNCSIG cout << __FUNCSIG__ << endl;

class A;
class B;
class A
{
private:
	unique_ptr<b> b;
public:
	A();
	void set_B(unique_ptr<b>&& _b);
	void do_something_with_B();
	void foobar_A();
};

class B
{
private:
	unique_ptr<A> a;
public:
	B();
	void set_A(unique_ptr<A>&& _a);
	void do_something_with_A();
	void foobar_B();
};

A::A() : b(nullptr) { FUNCSIG }
void A::set_B(unique_ptr<b>&& _b) { b = move(_b); }
void A::do_something_with_B() { FUNCSIG b->foobar_B(); }
void A::foobar_A() { FUNCSIG }

B::B() : a(nullptr) { FUNCSIG }
void B::set_A(unique_ptr<A>&& _a) { a = move(_a); }
void B::do_something_with_A() { FUNCSIG a->foobar_A(); }
void B::foobar_B() { FUNCSIG }

int main()
{
	cout << "Hello World\n";
	A a;
	a.set_B(make_unique<b>());
	B b;
	b.set_A(make_unique<A>());
	a.do_something_with_B();
	b.do_something_with_A();
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 14-Aug-21 8:24am    
I corrected it by fixing the formatting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900