r/cpp • u/tartaruga232 MSVC user, r/cpp_modules • Mar 26 '26
Module partitions can be tricky to use. How tricky do they need to be?
Which code example would you prefer? (If you could choose...)
Example A
// file bar.cppm
export module foo:bar;
...
// file bar1.cpp
module foo:bar.impl1;
import :bar;
...
// file bar2.cpp
module foo:bar.impl2;
import :bar;
...Note: Neither foo:bar.impl1 nor foo:bar.impl2 are imported anywhere, so generating a BMI file for these is pointless.
Example B
// file bar.ixx
export module foo:bar;
...
// file bar1.cpp
module foo:bar;
...
// file bar2.cpp
module foo:bar;
...Example A is what is currently supported by the C++ standard.
Example B is what is possible using the current implementation of the Microsoft C++ compiler (not yet in the standard).
Both variants achieve the same goal.
Which version do you think is easier to understand?
Previous related posts:
17
Upvotes
2
u/tartaruga232 MSVC user, r/cpp_modules Mar 26 '26
Perhaps the supporters of Example A could use a GUID-generator for those unused, artificial partition names:
This would reduce the risk of accidental name clashes. :-)
Personally, I do prefer to do it the Microsoft way (Example B).
4
u/GabrielDosReis Mar 26 '26
The standards have way too many sorts of partitions. If one is going to need a BMI in order to use a translation unit to transport information from one TU to another then, for all practical purposes, on has a module whether it is re-exported or not. But I see why they did that. MSVC's implementation is to provide both semantics, and give the choice to the user to select which ones they want. I rarely use internal partitions; I recommend against importing them interface partitions because that creates an interface dependency, so it is not really hidden or internal in any practical form.
1
u/tartaruga232 MSVC user, r/cpp_modules Apr 01 '26
See also my new Posting "Simple Rules for Partitions".
1
u/38thTimesACharm Mar 26 '26
At first glance Microsoft's seems cleaner, however there is one problem. Sometimes you do want to import a module that lacks the export keyword, having it function sort of like an internal header file. So they have two different uses for implementation partitions that require compiler flags to distinguish, which isn't ideal.
The standard's way simply says "all partitions can be imported," which is how it is with headers (nothing stopping you from
#includeing a .cpp file.)0
u/[deleted] Mar 27 '26
[deleted] — view deleted comment