r/cpp • u/mropert • Apr 13 '26
Can we finally use C++ Modules in 2026? · Mathieu Ropert
https://mropert.github.io/2026/04/13/modules_in_2026/
132
Upvotes
r/cpp • u/mropert • Apr 13 '26
3
u/light_oxygen Apr 14 '26
All these years, still reading this as Mathieu Report 😁😁😭😭
19
u/delta_p_delta_x Apr 13 '26 edited Apr 14 '26
As the implementer and maintainer of
vulkan.cppmprovidingmodule vulkan, I have a few notes as to why we made these changes, especially for u/mropert (we met in London last year).Primarily, Vulkan-Hpp exports
std::symbols in its public namespace—either as subtypes ofstd::classes, functions accepting and returningstd::types (or said subclasses), or types containingstd::types themselves. From a modules perspective, merely#includeing dependencies results in an ill-specified dependency chain. The standard, specificallymodule.global.fragillustrates that symbols in the global module fragment—where the Vulkan-Hpp primary module interface previously had its#include <stdlibrary>lines—are not necessarily reachable in consumer code.Symbol visibility vs reachability wasn't really explored in C++ prior to modules. This issue repeatedly manifested when working with
<compare>on MSVC. For instance, users had to#include <compare>to use some parts of Vulkan-Hpp properly (otherwise it would fail with an obscure compiler error), and this was deemed a leaky and broken abstraction. The suggested fix was to writeusing std::compare, but then it'd fail for some otherstd::type or function, and we'd have to add essentially every standard library type and function.This was deemed intractable. The alternative and more complete solution is to just demand
import std, which is what we did. Clang was (is?) looser with this particular semantic, so it wouldn't error out. So, if you writeimport vulkan;now, you getimport std;for free, just like how you'd previously get#include <array> #include <vector> #include <...>.Clang calls this the Non-ABI-breaking
export extern "C++"style. There are more, and if you are always building as a module, then why not make everything native modules? Glaze, a C++23-only library, is doing this.Modules crystallise unity builds and PCHs into native language-supported behaviour, instead of relying on build system hacks. I would go so far as saying modules entirely obviate both TU-local declarations (ie
static) and anonymous namespaces (ienamespace {}), and you can get this encapsulation by simply... not writingexport, and bam, you have module-only reachability.Additionally, u/not_a_novel_account would say (and I agree with him) that the C++ mentality of breaking a library into multiple TUs is decidedly pre-module thinking. With modules, each library should be one giant module with one giant TU (easy example: look at
std). The BMI phase handles the name lookup, there is no longer a giant header-parsing phase, and linking is much faster.This is a hard compiler problem; the solution is to write
import dependency_module;after all headers, and in module interface units, write it after yourexport module mymodule;statement. (In module interfaces you get the GMF where headers should go, anyway.) That way, if you want to make your dependency transitive, it is straightforward to addexportin front of thatimport.9
u/misuo Apr 13 '26
Betteridge's law of headlines
145
u/Mango-D Apr 13 '26
Just 50 more years bro
52
u/Minimonium Apr 13 '26
If anyone managed to make clangd not choke on very basic modules I would appreciate the insight. Tried to convert one library in my project around January, and while it compiles marginally faster in incremental builds, all "productivity" is gone because tooling just doesn't support it.
21
u/tudorb Apr 13 '26
Betteridge’s Law of Headlines applies.
16
u/slithering3897 Apr 13 '26
Yes... the C++ ecosystem has been waiting all this time for modules to become usable. One of the hardest problems in computer science. Then EDG decided to exit. Any updates to Intellisense are very rare. They've got to do something. What about reflection?
For me though, I get the different problem in that std stuff gets too many red squiggles:
The build system does better, but there's some basic issues. Like having more than one module in a DLL: https://developercommunity.visualstudio.com/t/C20-Modules-Spurious-warning-LNK4217/10892880.
And there's this rejects-valid error which used to be an ICE: https://developercommunity.visualstudio.com/t/Modules-ICE-when-using-constraint-on-fri/11022749.
And the debugger's natvis sometimes doesn't work: https://developercommunity.visualstudio.com/t/C-modules-debugger-cannot-inspect-vari/10981875
Standard-wise, I would like to see some change so that you can have different importable interfaces under the same module name mangling. It could be done though "submodules" or "public partitions". And, "anonymous partitions" looks like a good idea (or, don't implicitly import the primary interface, but that would be a breaking change).
At least I can go all in on modules once they finally become usable. I can just write wrapper modules for external dependencies.
28
u/xaervagon Apr 13 '26
It feels like Microsoft tried to help out by porting their C# namespace system to C++, but the python wonks wanted it to be more like the package management there so we wound up with a compromise that makes nobody happy.
24
u/pacafan Apr 13 '26
Every time I look at modules I want to cry. It seemed like modules would have the perfect opportunity to optimize / trim the language a little bit and get rid of cruft because the shift to modules is so major you might as well get rid of obselete or terrible patterns.
But no.
Modules just created a whole monster by itself.
Whenever somebody wants to have an example of what "Designed by committee" means... Modules are definitely it.
I want to cry.
4
u/revenantname Apr 14 '26 edited Apr 14 '26
Something I've struggled with regarding modules is how to deal with exporting interfaces that use symbols from headers. And I'm not talking about exporting to consumers, I mean just within my project.
For instance if I try to export a function with a symbol from a library header (e.g. nlohmann::json) as the parameters or return type, I'll get an error about something to do with the interface using a non-exported type. This makes sense, I shouldn't be able to export something that's not explicitly marked for export. But then how am I supposed to create cross-module interfaces based on these types?
I can see the argument that maybe you should wrap up these types when part of your external interface, but how should I deal with this internally? If I have partitions my_internal_module:class_a and my_internal_module:class_b, how should I pass a nlohmann::json object between them? Am I just structuring my modules incorrectly?
Also, I think this is what header units are supposed to solve? I'm not quite sure though since support for them seems lacking and based on what I've read the implementation of the feature isn't trivial either.
7
u/alex-weej Apr 13 '26
lol no
9
u/current_thread Apr 13 '26
For my game I recently switched away from MSBuild/ MSVC on Windows to CMake/ Clang on Arch Linux. It worked well for three weeks, until
pacman -Syumessed up my whole build. At this point I'm just tired that things don't just freaking work.To add insult to injury GCC complains about different things than clang. Clang used to work but now throws internal compiler errors. Boost.container worked well wrapped in a module until suddenly
operator newisn't found by clang anymore.Clion is an absolute mess and basically unusable with modules no matter which analysis engine I'm using.
Like, don't get me wrong: I know bleeding edge stuff can be broken, but this feature has had so so so so so so long to gestate and somehow support still is basically non-existent. At the same time I don't really wanna go back to headers either. I really like the encapsulation that modules provide.
2
u/enl1l Apr 15 '26
clangd support for modules is not perfect even though it somewhat works. I'm keeping track of this project https://github.com/clice-io/clice, which is aiming to write a new lsp server from scratch with module support as a priority
4
u/MT4K Apr 13 '26
I wonder whether a completely new approach to C++ modules, designed from scratch and better suitable for implementing in real-world compilers and tools, would make sense. Like the XHTML2 standard that was never implemented and was replaced with more practical HTML5.
3
u/caroIine Apr 13 '26
I just want import std; let compiler build it itself it's the standard std after all. If you change your flags, build another one under a hashed name. If there is import std; ignore every other include std to avoid import/include conflicts. That's it you just speeded up every existing C++ project for free.
1
u/unaligned_access Apr 14 '26
Any thoughts about implicit/Clang modules as a migration path?
1
u/username000101_ Jun 05 '26
Clang 22 + CMake 4.X You can use .cppm files
-13
u/Sopel97 Apr 13 '26
C++ peaked with C++17. Everything I see about >=C++20 is either useless or unimplemented or a crippled 3rd party lib port. Back then I was hopeful about modules as that one thing that could be positive but since then I lost all hope in this language.
8
u/R3DKn16h7 Apr 13 '26
At this point we should just let modules die and not even try anymore... It was a great attempt and full of promise but to be honest causes more pain that it solves.