r/cpp • u/tartaruga232 MSVC user, r/cpp_modules • Apr 15 '26
How a Module Should Look Like
https://abuehl.github.io/2026/04/14/how-a-module-should-look-like.htmlThis illustrates the point using real code examples from the Core module of our UML Editor. The presented source code of our Core module is complete.
All published source code examples in the text or linked from it are 100% standard conformant. However, the code contains redundant imports. On purpose. Read the text for why!
(See also my previous posting "Let's bite the Bullet: Module Units shouldn't implicitly import anything" for context).
33
Upvotes
View all comments
Show parent comment
4
u/tartaruga232 MSVC user, r/cpp_modules Apr 15 '26 edited Apr 15 '26
Let's add some comments to your code, so we can identify your translation units:
TU #2 is an external partition. So far so good.
TU #3 is an internal partition.
Problem: TU #3 and #2 have the same partition name.
This violates https://eel.is/c++draft/module#unit-3 ("A named module shall not contain multiple module partitions with the same module-partition.")
Assuming you fix that, you need to give an arbitrary unique name to the internal partition in TU #3.
The partition #3 is then unused (it is not imported anywhere). Thus its BMI unused (and that work done by the compiler is wasted).
Also, you forgot to import
:P(TU #2) in #3. TU #3 needs the declaration of functionfoo(), because it implements that function.Then: Congratulations!
You have just re-discovered the messy internal partition pattern (see my previous posting), as described here: https://github.com/abuehl/docs/blob/main/no-implicit-import.md#a-messy-alternative
Which solves the problem of unneeded recompilations, but adds the following two new problems:
(as already mentioned)
Partitions have the property, that they do not implicitly import anything. But they are intended to be imported somwhere.
So you have just again discovered, that we need a module unit, which doesn't implicitly import anything.
The implementation units we already have in the standard (
"module M;") have the property, that you can have as many of them as you like, they don't have an individual name and they can't be imported, so no BMI needs to be produced for these.That's almost exactly what we want, but without the implicit import.