r/cpp • u/holyblackcat • Mar 14 '26
Why do all compilers use the strong ownership model for C++20 modules, instead of the weak model?
In short, the strong ownership model = all functions declared in a module are mangled to include the module name, while the weak ownership model = only non-exported functions are mangled this way.
All three big compilers seem to use the strong model (with extern "C++" as a way to opt out). But why?
I asked on stackoverflow, but didn't get a satisfying answer. I'm told the weak model is "fragile", but what is fragile about it?
The weak model seems to have the obvious advantage of decoupling the use of modules from ABI (the library can be built internally with or without modules, and then independently consumed with or without modules).
The strong model displays the module name in "undefined reference" errors, but it's not very useful, since arguably the module name should match the namespace name in most cases.
Also the strong model doesn't diagnose duplicate definitions across modules until you import them both in the same TU (and actually try to call the offending function).
Does anyone have any insight about this?
23
u/chengfeng-xie Mar 14 '26
The post Standard C++20 Modules support with MSVC in Visual Studio 2019 version 16.8 mentions MSVC's rationale for implementing strong module ownership:
As for ABI compatibility concerns, the talk C++ Modules Myth Busting mentions the MSVC linker switch
/cxxmodulestrongownership, which controls whether to emulate weak ownership with strong ownership, though I couldn't find public documentation for that switch.6
u/cpp_learner Mar 14 '26 edited Mar 14 '26
Neither does the weak model. In the weak model, duplicated class definitions might not be detected and result in a crash at run time.
5
u/jetilovag Mar 14 '26
Module names being mangled into exported entities allows consuming the same module twice, with different ABIs, back ends turned on/off. If not everything has their module names mangled, you'll likely end up with frankenstein executables and funny stack corruptions.
5
u/GabrielDosReis Mar 14 '26
See section 4.5 of P0142
13
u/not_a_novel_account cmake dev Mar 14 '26 edited Mar 14 '26
IIRC, it was considered a correctness problem for external sources to provide symbols which are exported by a given module.
I shouldn't be able to
export void foo()and be able to havefoosupplied from some random third-party vendor lib compiled in 1998. Avoiding this sort of collision was considered a goal of modules.Both strong and weak ownership have advantages and disadvantages. It was more important everyone settle on one than which one was picked.
With the advent of
extern "C++", we get both. You can be intentional about when you want to opt-out of the strong ownership model.58
u/erichkeane Clang Maintainer(Templates), EWG Chair Mar 14 '26
I don't work on modules, nor did I make this decision. How ever, I have worked extensively on Clangs manglers.
The one thing I have come to know: EVERY name will escape somehow, exported or not. So mangling "non-exported" names and expecting them to stay unique just doesn't work.
I would imagine that the folks who are working on modules are aware of this, strong ownership is a defensive choice that prevents breaking changes due to someone figuring out conflicts in the future.
41
u/Minimonium Mar 14 '26
The fact that the language allows both models is a huge misnomer.
It's very good that all relevant vendors came to the same model. Otherwise, users would not be able to rely on benefits of either one.
2
u/CarloWood Mar 16 '26
It makes perfect sense to me and I never even used models :/.
I think that using the weak model means using C linkage for exported functions, which allows for having those resolved during link time by anything: unrelated modules, modules with the wrong version, shared libraries that export using C linkage - whatever. Whereas the strong model uses C++ linkage which allows link time checking that your functions are actually provided by the intended model: the one that you compiled against/for.