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?
View all comments
Show parent comment
2
u/holyblackcat Mar 14 '26 edited Mar 14 '26
I don't really follow this. Strong ownership removes linker errors about duplicate defintions (moves them to compile-time if you happen to import both modules and call the offending function in a single TU), which is a bad thing, not a good thing.
The only way this could make sense is for inline functions, assuming they remain weak when exported from modules (but I don't know this is true on MSVC or not, I know it's true on Itanium). For those it removes the possibility that incompatible versions of a function are silently merged at link time.
They also speak about different modules exporting the same functions, and that it could be desirable to support that, but that makes little sense. This requires the modules to not have conflicting module names. If they they can do that, why can't they not have conflicting namespaces?