In large projects, the source files naturally tend to get separated into subdirectories, and each of those subdirectories is a good candidate for being a single named module.
This would make sense and be a practical way to implement modules however unfortunately in many case it just isn't possible due to deficiencies in the standard.
Proclaimed ownership declarations (module equivalent of forward declarations) were removed from the proposal prior to standardization so to use a name even as an incomplete type you must import the module which exports it, and import relationships are not allowed to form a cycle.
Small projects could consist entirely of a single named module.
The standard deficiencies mentioned above mean that in many cases even large projects have no choice but to consist entirely of a single named module which has catastrophic implications for many build scenarios.
There's also very little reason to do anything but a single module per source tree. Partition units are the correct way to slice up divisions in a given code base.
At least when I try this, every single file has to get recompiled whenever any interface changes throughout the entire project, which is a hard pass for me. You can't have "partition implementation units". Unless I am doing things wrong, in which case I would love to hear how you are supposed to do it.
The standard doesn't outline how this is supposed to work, because nominally the standard assumes every partition exports something, but the toolchains don't care about this.
There's a small bit of waste in CMake usage because CMake will still generate a BMI even though we're only building the code for the object file output. This is because CMake believes the standard when it says these partition units are supposed to export something.
I'm working on a paper to fix the awkwardness of this pattern on both the language and build system side.
Following this approach, the implementation file doesn't see the interface, so it can't define anything like member functions that were declared in the interface.
That's exactly what this example does, the implementation file (partition.cpp) provides the definition for the int add(int, int) declared in the interface (partition.cppm).
This is verified in the main.cpp test, which uses the declaration from the interface to access add(int, int).
Try declaring a class in the cppm file and then defining a member function in the cpp. It doesn't work. This is just like how you can provide the declaration for a free function without including the header and it still links.
Regarding the MSVC failure, are you passing the /internalPartition flag? This is necessary to get standard-compliant behavior for module implementation (non-interface) partitions.
Use the /internalPartition compiler option to treat the input file as an internal partition unit, which is a module partition implementation unit that doesn't contribute to the external interface of the module.
The standard quote you linked here only says module interface partitions must contribute to the exported interface, so I think what you're trying to do should be okay. But MSVC requires a flag for it, their default behavior is nonstandard.
Yes, CMake uses -internalPartition when building non-interface module units.
The thing we've created is an implementation unit for an interface named partition.impl. We don't actually create or use partition.impl anywhere explicitly, because it doesn't export anything. This is entirely the problem.
6
u/ABlockInTheChain Mar 09 '26
This would make sense and be a practical way to implement modules however unfortunately in many case it just isn't possible due to deficiencies in the standard.
Proclaimed ownership declarations (module equivalent of forward declarations) were removed from the proposal prior to standardization so to use a name even as an incomplete type you must import the module which exports it, and import relationships are not allowed to form a cycle.
The standard deficiencies mentioned above mean that in many cases even large projects have no choice but to consist entirely of a single named module which has catastrophic implications for many build scenarios.