r/cpp • u/tartaruga232 MSVC user, r/cpp_modules • Apr 17 '26
Question to Module Users: How Do You Use Partitions?
Quick question for those who currently use modules with partitions.
That is, for a module like the one below (our file Core/_Module.ixx):
export module Core;
export import :Attach;
export import :Base;
export import :Container;
export import :Diagram;
export import :Interfaces;
export import :Transaction;
export import :View;How did you implement your code?
What we did:
We have done it as I've described in my blog posting "How a Module Should Look", which is for example for our file Core/Transaction/FinalizerDock.cpp:
module Core;
import :Transaction;
...which contains function definitions for declarations in the partition Core/Transaction/Transaction.ixx:
export module Core:Transaction;
...Please tell me what you did. I'm really curious.
Thanks in advance for the responses!
12
u/GabrielDosReis Apr 17 '26
I put in the primary module interface only exports that truly need to be exposed to users / customers of the products. Everything else, I put in the implementation files. I pay extra attention to unnecessary interface dependencies. A tendency from the header world is to decompose too finely grained (possibly into partitions that translate into too large interface dependencies).
Some of the code (a very tiny fraction) became recently public. Since a good chunk of what I work on in day-to-day is brown fields, we are talking about migration or "upgrading" in existing projects while servicing value. I don't hesitate to use tools that take care of toil and help me move faster (and sometimes experiment in directions that are instructive to me and my colleagues). See for instance the PR https://github.com/GabrielDosReis/ipr/pull/323
That complete migration is not done yet as I got distracted by more pressing daytime job priorities.
4
u/id3dx Apr 17 '26
I don't use implementation files unless there's a good reason to. I try to keep all the code directly in module partition or internal partition files, selectively exporting only what's necessary. This way, you trade some compilation time for better code locality and much less duplication that occurs with separate declaration and implementation files. I use implementation files on larger projects if incremental build times become a nuisance, but on most other projects, I've preferred the deduplication.
6
u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Apr 17 '26
Sure, I did something similar when we designed a few new subsystems in an in-house project a few years ago.
But I restrained myself from being too granular when deciding on the composition I had in mind. Do the opposite: make things much coarser than you would usually do with traditional headers.
And don't forget about the less-commonly known tool in your modules toolbox: non-exported partitions a.k.a. internal partitions. They can serve as additional dependency roots in a module, thereby saving module implementation units from unnecessarily bulky primary module interface units.
1
u/Kridenberg Apr 17 '26
I am basically doing the same (a lot of partitions that are re-exportef from the "core" file). I like that approach, but there are some problems:
So, somehow, I find more correct & simple to subdivide big modules in smaller. This naturally solves problem 1, and also help for some backends in case of 2.
1
u/mwasplund soup Apr 21 '26
I have stopped having a separation between declaration and definition and instead organize my code to generally be a single named module for each static library which has many partition interfaces that create tight inter-dependencies and then the named module interface organizes what is public or internal based on what is re-exported. -> Example
2
u/pjmlp Apr 17 '26
Like this, https://github.com/pjmlp/RaytracingWeekend-CPP/tree/main/OneWeekend/RaytracingLib
However other projects don't use module partitions at all.
Other than that, I don't really have much use for modules outside my hobby projects, at work the highest we can go is C++17, and even if we could go higher, we are constrained by features that actually work in all target compilers.
At work C++ is something that we only reach for as complement to other programming stacks, due to bindings for existing SDKs, or OS/hardware features not exposed to the JIT/AOT toolchain.
1
u/fdwr fdwr@github 🔍 Apr 17 '26 edited Apr 17 '26
I don't, as I stay away from added complexity where possible 😅. Basically I map every old .cpp+.h pair to an .ixx file, rather than artificially trying to glom several classes into larger units that don't necessarily make sense under any one overarching name, to Gabriel's chagrin (trying to jam everything under one umbrella really falls apart though when you have a bunch of disparate utility classes shared across projects).
It is not ideal, but I'm sure the compilers will eventually add interface hashing to see if any meaningfully exported entities have changed between rebuilds to avoid transitive cascades from merely changing implementation details like function logic (currently, merely editing a comment invalidates the dependency tree :/).
Even with that nonidealness, finally ditching header/cpp synchronization is just lovely.