r/cpp • MSVC user, r/cpp_modules • Apr 26 '26

Code Examples From an App Using C++ Modules

https://abuehl.github.io/2026/04/26/code-examples-from-an-app-using-modules.html

This demonstrates usage of our recommendations for using modules:

  • Prefer small modules
  • Only use partitions if you really must
  • Avoid using internal partitions

The intention is to show some real code for context of discussions about using C++ modules.

My goal is not to sell modules. If you prefer not using modules: Use header files. There are good reasons to continue using them.

For me, C++ source code is an asset. The intrinsic value of code using modules is higher (lower entropy), as modules are a higher abstraction, compared to header files, which are basically just the pasting of C++ code into translation units (I've done that for ~36 years now).

Using C++ modules comes at a cost though. Understanding modules requires a certain level of understanding, which isn't needed when using the simple model and tradition of header files.

16 Upvotes

40 comments sorted by

View all comments

24

u/Jovibor_ Apr 26 '26

Prefer small modules

Worst suggestion out of all. import std; is laughing at you here.

Use modules of any size you need. That's the only correct advice on this matter.

9

u/jiixyj Apr 26 '26

My advice: Use one module per "library" or "package" (in the Lakos sense). So modules should be bigger things like std, fmt or boost.program_options, not down to the level of ".h/.cpp pair".

This matches similar advice from other people.

One big reason, especially if you are writing a library: The name of the module will be mangled into all your exported symbols. You would be exposing the internal organisation of your module to all your users:

// foo.cppm
export module foo;

export import foo.sub1;
export import foo.sub2;

// foo/sub1.cppm
export module foo.sub1;

namespace foo {
    export void f1(); // will be mangled approximately like `foo::f1@foo.sub1`
}

...so you wouldn't be able to move f1 from foo.sub1 to foo.sub2 without breaking the ABI of your module. If you want to keep the structure "secret" you have to use partitions (foo:sub1/foo:sub2).

Of course, if you're writing an application, you might have overriding reasons for doing smaller modules.

1

u/tartaruga232 MSVC user, r/cpp_modules Apr 28 '26

Just a random idea: For designing a big library, have you considered doing:

export module BigLib;  // interface of the BigLib library

import FantasyCorporation.Types1;  // not accessible to importers of BigLib

export namespace BigLib
{
using FantasyCorporation::FooBar;  // class FooBar from FantasyCorporation.Types1
...
}

1

u/tartaruga232 MSVC user, r/cpp_modules Apr 26 '26

If you want to keep the structure "secret" you have to use partitions (foo:sub1/foo:sub2).

I see your reasoning, but your conclusion is not really practical. The inner structure of a giant library at some point becomes unmanageable. Remember, there is no hiding between partitions.

Your requirement that there can't be an inner structure, is not manageable. The requirement to not see you moving f1 inside foo is arbitrary.

We currently have the situation that if a partition is changed, every implementation file needs to be recompiled. If you don't want that, use smaller modules or change the C++ standard. The latter takes too much time and has almost zero probability to succeed. In the mean time, I use smaller modules.

If tried that with our application. The time for a full build is roughly the same if I use a small number of big modules or many more smaller modules. I prefer the latter and have less recompilations when I change something.

5

u/not_a_novel_account cmake dev Apr 26 '26

Remember, there is no hiding between partitions.

There's no hiding between headers either. This is a non-goal.

It's all your code. We encapsulate things from external consumers of our code, not from intra-project.

-1

u/tartaruga232 MSVC user, r/cpp_modules Apr 26 '26

Your syntax "module M:;" or "module M:_;" will fail (I fear, but I wish you good luck with your proposal). The best bet is to wait until current internal partitions usage has died (perhaps in 10 years or so) and then reuse the syntax "module M:P;" (with the semantics of the Microsoft "extension"). That would be elegant and effective. But I will be retired if that manages to get in the standard.

6

u/not_a_novel_account cmake dev Apr 26 '26

The question of "how big should modules be" is completely irrelevant to the minor issues of implementation units. They're not related.

0

u/tartaruga232 MSVC user, r/cpp_modules Apr 26 '26

Current "module M;" causes lots of recompilations, if M has a big list of partitions. So for practical app development, the size of M matters. Smaller M, less code that needs to be recompiled.

5

u/not_a_novel_account cmake dev Apr 26 '26

Interfaces described in the PMIU should be implemented via implementation units, module M;, interfaces described in partitions should be implemented in the partitions.

See: https://chuanqixu9.github.io/c++/2025/12/30/C++20-Modules-Best-Practices.en.html#modules-native-best-practices

It says the same recommendations, namely:

A Project Should Declare Only One Module; Use Module Partition Units for Multiple TUs

and

Use Module Implementation Partition Units, Not Module Implementation Units, to Implement Interfaces

0

u/slithering3897 Apr 26 '26 edited Apr 26 '26

One big reason, especially if you are writing a library: The name of the module will be mangled into all your exported symbols.

Yes, as I say, this seems to be deficient design in the standard. The module that a thing is in is tied to mangling. It would probably be just fine to use partitions in a lib.

But for applications, and applications split into component libs, that is not the perfect world because of build cascade.

So I'd use smaller modules with extern "C++" to get my forward declarations back.

*Was there something wrong with what I said?

6

u/slithering3897 Apr 26 '26 edited Apr 26 '26

I'd reckon large external lib modules and small imports for in-development modules. To keep control of your build cascade.

3

u/tartaruga232 MSVC user, r/cpp_modules Apr 27 '26

Yeah. Not every module needs to imitate the giant, special case like the std module (which must be also available as header files). As I said, our recommendations are

  • Prefer small modules
  • Only use partitions if you really must
  • Avoid using internal partitions

Especially the last point. Our code is now free of internal partitions. We have no use for them. And we don't need the module anti-pattern.

In my experience, the time for a full build stays the same, no matter if I do a smaller number of big modules or a a lot more smaller modules. Keeping modules small in an app provides faster rebuilds when something changes. We do not cast the interfaces of our internal modules in stone.

5

u/mjklaim Apr 26 '26 edited Apr 27 '26

Use modules of any size you need. That's the only correct advice on this matter.

My experience with a 3 years modules-only project matches this recommendation.

EDIT: clarification that I agree with that quote, not OP's.

6

u/tartaruga232 MSVC user, r/cpp_modules Apr 26 '26 edited Apr 26 '26

Having a big library being an aggregate of smaller modules is fine. Our app is not a library. The frequency of changes to module std is almost zero. That's very different to what we do in our app.

2

u/Zero_Owl Apr 26 '26

While I don't know much about how big modules should be, why do you think that one big std module is a good example?

7

u/not_a_novel_account cmake dev Apr 26 '26

Because it demonstrates that the main disadvantage of large single headers, slow build time, does not apply to modules.

1

u/Zero_Owl Apr 27 '26

That is a very narrow way of looking at things. I'd argue that the main disadvantage of having large single headers is that they pack unrelated things in one place and hence should be decoupled. In other words, it is an organization problem not the build one. And programmer should care about organization first and build times second. And by that metric one big std module is a pretty bad example.

3

u/not_a_novel_account cmake dev Apr 27 '26 edited Apr 27 '26

Nominally we should have the entire world of C++ code available to us in every translation unit. Import and include are not isolation or encapsulation mechanisms, they are not tools for organization, that's what namespaces and internal linkage are for.

Import and include are practical considerations for the toolchain to figure out how to efficiently bring declarations into scope. If that efficiency wasn't a problem, there would be no need for them and everything in the environment would be available in a given TU.

If you ignore this framing and treat headers and module units as organization methods, you risk import ambiguities and link-time collisions.

2

u/Zero_Owl Apr 27 '26

Well, yes but we don't have a properly designed namespace structure in std (and in most cpp libs, tbh) so headers became the tool for isolation. So it is naturally for people to continue having this mindset and treat modules the same. Although, as you say, maybe we need to stop doing it but for that to happen the cpp devs should start using namespaces properly otherwise there is no choice but follow the pattern we so used to with includes.

1

u/not_a_novel_account cmake dev Apr 27 '26

Nothing in std collides with anything else in namespace std.

You can include every std header without any collisions.

This is why import std works at all.

If we had different headers which provided different std::map templates that would be very bad and I would be forced to agree with you. That is not the case in std and will inevitably cause link errors in any library which tries with its own namespace.

0

u/Zero_Owl Apr 27 '26

It collides with other entities from other libs. Using both boost namespace and std namespace will blow things up. The only way to not is to prefix everything or cherry pick only particular entities. That's a very inconvenient way of doing things.

0

u/not_a_novel_account cmake dev Apr 27 '26 edited Apr 27 '26

Boost doesn't put anything in the std namespace.

If you're saying you can't write:

using namespace std;
using namespace boost;

That's not a name collision. You've created a lookup ambiguity, not two types with the same name.

Do not write using namespace ...; in global namespace scope.

-2

u/Zero_Owl Apr 27 '26

What does it matter how you name it? You understand perfectly what I said. Now please tell me how can I bring in my TU all math functions with import std and make sure it won't collide (create ambiguity, if you wish) with anything math unrelated by using just one line of code? That's something any modern language is capable of, btw.

→ More replies

2

u/tartaruga232 MSVC user, r/cpp_modules Apr 27 '26 edited Apr 27 '26

While I don't know much about how big modules should be, why do you think that one big std module is a good example?

It's indeed a bad example. 99.9% of internal modules used inside an app do not need to cast their ABI in stone for ages.

"Small" is not an absolute number, but if people start lumping together things in a module which don't need to be together, modules are getting too big, hard to navigate und cause too many recompilations which aren't needed. Implementation files for modules (*.cpp) are marked with "module M;", which implicitly imports the interface of M. Smaller interfaces means a smaller number of cpp files that need to be recompiled, if the interface changes.

We have some reasonably sized modules in our App package at https://github.com/cadifra/cadifra/tree/2026.6/code/App. Previously, we had a single module for the App package. Splitting it up didn't have any noticeable effect on the time required for a full build.