r/cpp_questions 9d ago

OPEN What’s the state of modules

I’ve been hearing about modules since a while now and I can’t seem to find any serious project on the internet using modules. I assume compilers still don’t fully support modules…

6 Upvotes

13 comments sorted by

View all comments

1

u/DDDDarky 9d ago

I don't think older serious project will just rewrite everything. Even for new project, I've looked a little bit into it, still I'm not 100% sure if modules are ready to be fully used instead of headers and if it's reasonable to do so (if someone does know feel free to reply), but since the major compilers don't even have full support I'm not considering using them as of now anyways.

1

u/No-Dentist-1645 8d ago

Most compilers have nearly full module support nowadays, including MSVC, GCC, and Clang. There may be some time edge cases, but that's true in general and doesn't really affect 99.9% of use cases (none of them are technically fully C++20 compatible, even excluding modules)

0

u/DDDDarky 8d ago

still, they are marked as experimental, except for msvc, I don't think most people are going around "hey, let's build our entire codebase around this experimental feature!".

But let's pretend I'm fine with that, if I remember from the last time I looked at them, I think there were some weird quirks such as around circular dependencies, and things that resulted basically in writing huge modules instead of fragmenting the code into many smaller modules, which seems to me like it sort of defeats the purpose, but again I might be misremembering things, I have not looked deep into it yet.

3

u/tartaruga232 8d ago edited 8d ago

The "big modules" thing is a red herring some people keep repeating. We tried that but we saw no benefit with big modules. We now have lot's of small modules and see no drawback in doing that. Rather the contrary. Small modules make it easier to have the interface and the implementation in the same file, which enables the compiler to read one file and produce the BMI of the interface and the obj in one go.

You cannot have circular dependencies. If you have classes that refer to each other - even if it is only by pointer or reference - they have to be in the same module (example). If the source file gets too big to handle that, you can split it using partitions and use forward declarations across partitions. But I would avoid using partitions if you are not forced to use them. Some people seem to be using partitions as a means for hiding the internals of a module. We just expose small modules across sub-systems. Some are for internal use only, some are for usage across subsystems.