r/cpp • • 4h ago

What do you think about modules?

I see lot of people don't like modules how they are now can they can be improved and what can be suitable solution what do you think?

6 Upvotes

30 comments sorted by

23

u/TTachyon 4h ago

I think people's problem with modules is that they don't work (well) enough for that they need, either because they have to use old tools, or the new ones aren't good enough yet.

•

u/tartaruga232 MSVC user, r/cpp_modules 3h ago

They work and we use them. They are useful.

What's missing is, that you cannot do forward declarations of classes which are defined in foreign modules. You need to import the whole module, even though an incomplete type declaration would do. This creates unneeded dependencies and forces a lot of thinking.

•

u/javascript 3h ago

Modules only make sense in a world where C++ as a community is willing to break backwards compatibility. Having two import systems in one language is poor form.

17

u/Jakabxmarci 4h ago

We transformed part of our cosebase to use modules, but we regretted it afterwards mostly due to missing tooling support. CMake is mostly ok now, but distributed compilation, ccache, lots of libraries etc. doesn't work. If you do a smaller project without external dependencies then I'd recommend it.

•

u/GunpowderGuy 3h ago

what languages dont work once you use modules? in what way? isnt code that uses modules supposed to be able to use code that doesnt use modules?

•

u/Jakabxmarci 2h ago

yeah they still work, but having both imports and includes is not great

•

u/eyes-are-fading-blue 3h ago

I truly wonder what kind of analysis was done to justify this?

•

u/Jakabxmarci 2h ago

we needed faster compile times...which we got, but also a lot of headache

•

u/eyes-are-fading-blue 2h ago

How long was your compilation time with headers?

•

u/steveire Contributor: Qt, CMake, Clang 2h ago

I won't really know what to think about them until libraries using them are packaged in linux distros. I'm interested to see how convenient they will be to use then. 

•

u/holyblackcat 3h ago

The implicit import A; in any module A; sucks, but otherwise the design looks ok to me. I wish compilers implemented the proper scanning for header units to make them usable.

•

u/AvidCoco 1h ago

They don’t solve any problems (for me).

•

u/fortsnek274 3h ago

Pros:

  • Faster compile times. Simply by import std.
  • Build systems could perform hashing on the interface to prevent build cascade.
  • Ergonomics of export. Hide implementation details.
  • No macro leaks.

Cons:

  • When it doesn't work.
  • External libs that are not yet modularised. You'll need to write a wrapper module for them if you want one.
  • Cannot import specific partitions. So you have to import the whole module and depend on all partitions even if you only use some tiny detail of one partition. That's more build cascade. Aka, lack of package/submodule system.
  • Cannot forward declare with proclaimed ownership.
  • Implementation files take a dependency on the whole module instead of relevant partitions (there's a paper for this).
  • Compilation would probably be faster if modules could be looked up by filename.
  • Module name is another source of repetition (for headers, it could at least be implicit by file path). This was solved in some presentation somewhere...
  • Template instantiations are not reused as much as I'd like.
  • They can't export macros. But you can have a header that just does the import and includes the macros.

•

u/azuled 2h ago

This is an outsider perspective, possibly. I don't use c++ very often, but I try to keep myself aware of how to use it properly, and every once in a while I write a project in it.

Modules seem like a massive improvement to the developer experience in theory. In practice it was a legitimate nightmare to get them working the last time I tried (June/July of 2026). I like the cleaner namespace separation, I like the cleaner import logic, I like the simpler boilerplate on every file. But trying to actually use modules took more time than the actual project took!

If they ever reach maturity, and ship with gcc/llvm+clang then yeah, I'm all in, I'll swap immediately, but it feels not ready yet.

•

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions 58m ago

I really like the power that the export keyword gives you that you don't really have with headers.

I just recently moved over to CMake 4.5.0 and now I can use import std pretty cleanly. So I'm pretty happy with the modules at this point. I don't think my builds are any faster, and I think they might even be a bit slower due to how clang does its dependency scanning, but I really appreciate the way that my code is looking, the way my code is organized, and the ability to selectively export particular symbols.

4

u/UndefFox 4h ago

My only problem with them so far is lack of tooling support. Qt Creator doesn't support them properly so all syntax highlight is broken.

Also the fact that modules will make more people use single file for everything. It's cool that you no longer have to repeat function declaration, but also reduces readability of the classes drastically...

3

u/ludonarrator 4h ago

Not sure what you mean, you can still separate them if you want:

// interface
export module foo;

export namespace foo {
void bar();
}

// implementation
module foo;

void foo::bar() {}

1

u/Wonderful-Habit-139 4h ago

I think they mean defining things in too many files.

1

u/UndefFox 4h ago

You can. The point is: if there less cost for keeping things in the same file, more people will do it. Main reason for separation was compilation advantages. With modules, it basically becomes stylistic choice as far as I know.

2

u/ludonarrator 4h ago

It's not entirely stylistic: in the above example, importers of foo will not need to be recompiled if the implementation changes, unlike if it were inline in the exported interface.

•

u/UndefFox 3h ago

Afaik some compilers don't recompile binary api if you change non exported methods, hence removing this factor.

•

u/fortsnek274 2h ago

That would be news to me. Which build systems have module smarts? They would need interface hashing.

•

u/not_a_novel_account cmake dev 1h ago

Bazel naturally takes advantage of this, although it has other drawbacks.

5

u/current_thread 4h ago

Among many other languages both java and C# have made this work for decades, so I'd wager it's just a matter of getting used to it

2

u/UndefFox 4h ago

Idk... I love cpp, but have to work with Python more and more often recently because people are asking for help. The moment python project becomes more than 200 lines, I start slowly getting headache... same with Zig. So nah, I think having a big overview of the interface is quite good.

Otherwise... probably time to write some scripts that will allow me to see the exact same thing and only show the parts I care about/

•

u/chibuku_chauya 2h ago

What would be nice is a way for the compiler (or build system) to automatically scan exports and produce an interface file at build time. All the information it needs is already there. It just needs automation.

•

u/eyes-are-fading-blue 3h ago

I gave it a go. It was too buggy back then. The interaction with headers was also a bit cumbersome to work with. I am waiting for tooling maturity at this point.

•

u/pjmlp 3h ago

I like them, and use them on hobby coding, only because my hobby coding is mostly focused on Visual Studio and Windows.

However we are still not there if you care about portable cross platform code.

I think they are the best example of what happens when ISO C++ keeps ignoring how the language magically transforms into executable code as part of the standardisation process.

-21

u/[deleted] 4h ago

[removed] — view removed comment

•

u/James20k P2005R0 51m ago

Don't be like this