sigurd4
sigurd4

Reputation: 45

Mutually referenced C++23 modules

I'm very new to using modules in C++. When i have two modules including each other, this results in a compile error.

Say, if i have a class in one file, say, meter.cppm, and another in inch.cppm, and an interface/trait for how the two units of length correspond to each other in unit.cppm.

Which files should include each other? I know how to solve this using plain old headers, but with modules this seems like a difficult task.

I've tried using headers, then re-exporting in a module interface, but this seems very impractical. What is the best way to do this?

Upvotes: 0

Views: 45

Answers (1)

Wolfgang Bangerth
Wolfgang Bangerth

Reputation: 226

Modules cannot depend on each other in a circular way. With traditional header files, you can solve that with forward declarations, but with modules you can't because you cannot forward declare things in a way that makes it clear that the declared entity is owned by another module.

The way to do what you want to do is to use "module partitions". In that case, you can put everything into one module, but the declarations and implementations of the two classes are split into different partitions. Within a partition, then, you can forward declare classes that are declared/defined in the other partition because everything that is part of different partitions is still owned by the same module.

Upvotes: 0

Related Questions