r/cpp • • 3d 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?

29 Upvotes

71 comments sorted by

52

u/TTachyon 3d 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.

40

u/Jakabxmarci 3d 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.

2

u/GunpowderGuy 3d ago edited 1d ago

what libraries 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?

2

u/saxbophone mutable volatile void 1d 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?

Surely you meant what libraries?

3

u/Jakabxmarci 3d ago

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

1

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

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

13

u/Jakabxmarci 3d ago

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

3

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

How long was your compilation time with headers?

17

u/kammce WG21 | 🇺🇲 NB | Boost | Exceptions 3d 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.

33

u/tartaruga232 MSVC user, r/cpp_modules 3d 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.

10

u/staletic reddit thinks /u/_bstaletic is a bot 2d ago

What's missing is, that you cannot do forward declarations of classes which are defined in foreign modules.

I don't expect this to ever be resolved. The standard does not prescribe that module names should be considered when mangling names. That gives rise to two models, colloquially dubbed "weak ownership" and "strong ownership" model. With strong ownership, you get protection against ODR violations coming from two libraries defining the same type. Both get their module names mangled into the type and everyone is happy. The weak ownership on the other hand maintains the ability to declare forward decls for someone else's module, and, in general, ignoring the ODR violations that were already there.

Very early on, MSVC opted for strong ownership and GCC and Clang for weak ownership. I remember r/cpp being pretty vocal about strong ownership being the right choice. Very quickly, GCC and Clang switched to the strong ownership model. Whether r/cpp had that much sway... I doubt it.

 

I also don't think it was ever a good idea to declare forward decls for someone else's symbols. One can do

namespace std { template<typename T> class vector; }

But that's not the correct forward declaration. First, std::vector has an allocator type as well. Second, at least libstdc++ uses an inline namespace as well. Something like

namespace std::inline __v1 { template<typename T, typename A = std::allocator<T>> class vector; }

A better option would be to request forward declaration headers to be provided by thte 3rd author/maintainer (yes, that still leaves the module world).

7

u/tartaruga232 MSVC user, r/cpp_modules 2d ago

I don't think it has to do with weak/strong ownership. A redditor in the past mentioned that there was even a proposed syntax for how to do forward declarations of symbols in foreign modules. But they removed it from the standard. I need to dig that out...

4

u/staletic reddit thinks /u/_bstaletic is a bot 2d ago

Well, if you extend the language with new syntax, sure.

2

u/gracicot 1d ago

That would be p0787

0

u/GabrielDosReis 1d ago

Proclaimed ownership declarations were in the Modules TS. They didn't make into the IS, for good or bad. Over time, I think that was a right call.

2

u/ABlockInTheChain 1d ago

I also don't think it was ever a good idea to declare forward decls for someone else's symbols.

There are a lot of problems that can arise from forward declaring templates that do not arise from forward declaring non-template types.

Using the issues from the former to justify throwing out the latter is a mistake.

2

u/James20k P2005R0 18h ago

This, forward declaration of structs/classes is an incredibly useful way to reduce compile times. Something like Godot doesn't provide forward declaration headers, but you can just:

namespace godot
{
    struct Node;
    struct Node3D;
    struct Camera3D;
    struct PackedScene;
}

Which cuts down on compile times, and doesn't generate any problems

3

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

This, forward declaration of structs/classes is an incredibly useful way to reduce compile times.

Not just that, but having to import a module just because you use a pointer or a reference in a interface makes designing with modules very hard.

I was pretty surprised to learn that modules don't support forward declarations of classes/structs across module boundaries. I then spent a lot of time to redesign our code base. There seem to be people who think that forward declarations of classes are evil anyway, but that's not true.

Importing a module just because you want to use a pointer or a reference to a class from that module is against what we have done for decades in C++ without any problem. Sure, importing a module is cheap with regards to compile time, but it is still a dependency.

I believe the lack of support for that in modules is likely a huge reason for the very slow adoption of modules we have seen over the recent years now. Which is a pity, because modules are a pretty nice feature otherwise.

0

u/ABlockInTheChain 8h ago

Importing a module just because you want to use a pointer or a reference to a class from that module is against what we have done for decades in C++ without any problem. Sure, importing a module is cheap with regards to compile time, but it is still a dependency.

This is the single most important blocker for why I don't want any of my dependencies to modularize.

We already solved the build speed problem for third party headers years ago by enforcing a convention of #include <our_project/external/third_party_a.hpp>, #include <our_project/external/third_party_b.hpp>, etc. and then adding those headers to target_precompile_headers.

Modules only solve a build speed problem we don't experience, and add theoretical macro isolation to fix problems that we've never actually seen in practice while making dependencies like certain boost libraries more difficult to configure.

All downside, no upside.

If modules at least didn't break non-template forward declarations they would shift from "actively harmful" to "net neutral".

27

u/javascript 3d 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.

5

u/Valuable_Leopard_799 2d ago

Tbh the way modules can import headers is very cool. .

6

u/javascript 2d ago

Valuable for a migration path, but eventually the plan should be to deprecate and remove includes. There isn't any interest in that right now.

5

u/NilacTheGrim 2d ago

Yeah, buddy. That's never gunna happen. Nor should it. Trivially easy C interop is crucial for C++.

7

u/javascript 2d ago

Interop isn't the same as syntactic superset. There are other strategies

2

u/NilacTheGrim 2d ago

Not for C++. That ship has sailed. C++ is the mostly superset of C language. If you dislike that don't use it. Use the other strategies languages like Rust or Swift or whatever.

9

u/gmes78 2d ago

Other languages can import C headers without a literal equivalent to the #include preprocessor directive.

4

u/pjmlp 2d ago

That honor actually belongs to Objective-C, which is always full ISO C, plus the Smalltalk like features on top.

So in Objective-C you can use any C language feature up to latest standard, C23 nowadays, unlike C++ which remains mostly C89, with the C standard library parts that don't conflict with C++ language features, or require C language constructs not accepted by C++.

-2

u/NilacTheGrim 2d ago

^ Exactly this.

13

u/fortsnek274 3d 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.

9

u/holyblackcat 3d 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.

4

u/staletic reddit thinks /u/_bstaletic is a bot 2d ago

There's a paper addressing this. Let me find it...

https://wg21.link/p4376

3

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB 2d ago

Right. So far, as presented last week, it was more like testing water temperature. It needs far more than just throwing around ideas.

1

u/kamrann_ 2d ago

So what was the temperature? Because I'm yet to hear any reasoning for why it was designed this way in the first place. And if there is no good reason, then surely it should be changed as proposed because as things are it's basically a case of saving typing a few words and getting massive unnecessary build dependencies in return.

10

u/UndefFox 3d 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...

5

u/ludonarrator 3d 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() {}

2

u/UndefFox 3d 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.

5

u/ludonarrator 3d 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.

2

u/UndefFox 3d ago

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

3

u/fortsnek274 3d ago

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

5

u/not_a_novel_account cmake dev 3d ago

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

-1

u/Wonderful-Habit-139 3d ago

I think they mean defining things in too many files.

6

u/current_thread 3d 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

5

u/UndefFox 3d 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/

2

u/chibuku_chauya 3d 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.

1

u/GabrielDosReis 1d ago

I agree such a tool would be useful. In fact, very early on in the Modules TS development, a former colleague of mine wrote such a tool out of the IFC produced by the compiler. It is useful, for instance, when bootrapping a system - the first phase may use stubs whereas the final phase uses the real definition; the rest of consumers just see minimal interface when computing dependencies.

There are some tricky corner cases if you insist on completness of support for all the creative ways people write C++.

4

u/azuled 3d 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.

4

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB 2d ago

With projects in production (like e.g. from our company), I see Modules as kind of 'legacy' now. Hence, it needs very solid and convincing ideas for potential improvements to convince me of changing the standard. On top of that: you need to convince the implementers and tool suppliers to collectively agree on that, too.

4

u/Jakka47 2d ago

Once you've worked out how to use them, you'll never want to go back. I'm working on a codebase that uses a mix of modules and includes. Eventually it will all use modules. Had a few issues with compiler bugs at the start, but very few problems now. There are a few things you can't do with modules and some parts of the code were riddled with macros and conditional compilation, and had to be rewritten. TBH, it was worth it just to clean it all up. Some things should just not be allowed, but I do sympathize if you're stuck with legacy code you can't modify.

5

u/steveire Contributor: Qt, CMake, Clang 3d 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. 

6

u/AvidCoco 3d ago

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

3

u/a_jasmin 2d ago

For me the appeal is to stop having to maintain pairs of .cpp and .hpp files and do away with proprietary pre-compiled headers mechanism.

Current tool chains and libraries aren’t quite there yet unfortunately.

3

u/mjklaim 2d ago

I've been using modules for years in my personal projects (mostly games or plugins that are parts of game projects) using build2. I will probably never get back to includes as long as I can avoid it.(I'm ok using dependencies that are not modularized, I can modularize them specificallly for the project.

The short story: I'm very happy with it as long as I don't hit compiler crashes (ICE) or bugs specific to modules, but these days they are rarer. I use msvc and clang, but I've also used gcc.

I can use modules because my projects are isolated, but the choice of using them depends a lot on the tooling and if you're adapting an existing project or not, and if you expect your "users" can use an adapted toolchain, if you make libraries.

2

u/ConsumeUranuim 2d ago

I wrote some of my own tooling to work with them. Which definitely made them much more enjoyable to work with, to the point where my new codebases are almost only module-based now.

2

u/eyes-are-fading-blue 3d 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.

2

u/kamrann_ 2d ago

The unfortunate thing is that modules could solve a lot of problems for a lot of people, in so far as it's really difficult to overstate just how negative the effects of the preprocessor are on tooling. You might think headers work fine and believe that you rarely if ever are affected by ODR violations, but even if that's the case, there would be so much better tooling if not for all the pain that the preprocessor adds to the mix.

But of course, deprecating headers is a pipe dream. Modules feel like the perfect demonstration of how the need for backwards compatibility just cripples the potential of most new additions to the language.

0

u/pjmlp 3d 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.

1

u/saxbophone mutable volatile void 1d ago

They seem really great on paper but they've been thoroughly hobbled by lack of vendor support.

-1

u/Sopel97 2d ago

made me switch to rust

because it uncovered how messy the language is and that there is no path to fix that

0

u/ComedianMuted5486 2d ago edited 2d ago

It's one of those situations where C++ would probably benefit from having a "reference" compiler like Rust does. You could smoke out the issues with the standard much earlier, get users using it much earlier, have at least one reliable implementation earlier.

Instead of this weird cycle where things get standardized first before anyone bothers to get the implementations beyond half-assed, at which point issues get discovered and it's too late to fix them (without returning to the standards bodies)

0

u/pjmlp 2d ago

This has nothing to do with having a reference compiler, other languages do exist, with ECMA or ISO standards, where only field tested features get into the standard, not the waterfall approach from C++.

One such example are Web standards like JavaScript, or WebAssembly, where at least two implementations much exist, before landing into a new standard revision.

Even C is rather conservative on how features land into the standard

1

u/ComedianMuted5486 2d ago

Yes but the point is there isn't a designated place for experimental features to be tried, and so the major compilers often don't want to deal with them until it's standardized.

The situation with web browsers is a bit different since the browser vendors have more of a vested interest in pushing their own features forwards.

Major C & C++ features are not really driven by the compiler teams in the same way.

-2

u/pjmlp 2d ago

Most C features since C89, are either existing practice, or they got back ported to so speak, from C++ compilers field experience back into C.

The whole point of standards used to be to unify existing language extensions that were in common use.

Not waterfall language design.

In fact the very fist time waterfall design was attempted at ISO, it went famously bad, with ALGOL 68.

-2

u/NilacTheGrim 2d ago

Unneeded and stupid. I quietly hope they will just prove to never be viable and that they go away forever eventually and get removed from the language in like c++2037 or something.

0

u/all_is_love6667 2d ago

I don't know that much about modules.

I just know they're not really mature now, and apparently they don't really speed up compilation that much.

I sure hope it's going to improve in the future, it's probably a matter of how compiler engineers will update their compilers.

0

u/ir_dan 2d ago

Haven't used professionally. Annoyed about incompatibilities with headers, since modules aren't even close to being standard across compilers, and many projects are and will use headers for a long time. Not solving any real problems yet.

0

u/umlcat 2d ago

To be honest, I have a lot of trouble understanding how they work ...

0

u/megayippie 1d ago

They have so far been a great time sink with zero use-cases for. It seems like a completely failed idea in practice, honestly. I really want the promise of "import std" but as long as the support is not uniform in the major 3, and things like mixing includes and imports work, it is dead

-1

u/el_toro_2022 1d ago

if you look at the equivalent of modules in nearly every other language out there, the implementation is much cleaner. Haskell, Rust, Ruby... very usable.

The problem with C++ is that it still carries the legacy of header files and backwards compatibility. So it makes the implementation of modules that much more complicated, which is why it has not settled yet after how many years?

All my current C++ projects are header-only, which eliminates the messiness of splitting up your classes and structs into .hpp and .cpp files, which works and is fine for smaller projects, but for bigger ones can result into long compile times for the translation units that now must pull everything in again for even a small change somewhere.

There are ways around that hit, of course, but results in even more complexity in the build process.

I love C++, but the header files has been the biggest wart in my experience.

-22

u/[deleted] 3d ago

[removed] — view removed comment

-2

u/James20k P2005R0 3d ago

Don't be like this

-1

u/t4th 2d ago

I will try, but because C++ has C backward compatibility enabled by default you have to use defensive programming all the time to prevent bugs anyway.