r/cpp • u/tartaruga232 MSVC user, r/cpp_modules • Mar 16 '26
Implementation of Module Partitions and Standard Conformance
I've spent more than a year now using modules and I found something puzzling with partitions.
I'm using the MSVC compiler (VS 2026 18.4.0) with the following input:
// file A.ixx
export module A;
export import :P0;
export import :P1;
export import :P2;
// file AP0.ixx
export module A:P0;
export struct S
{
int a;
int b;
};
// file AP1.ixx
export module A:P1;
import :P0;
export S foo();
// file AP2.ixx
export module A:P2;
import :P0;
export S bar();
// file AP1.cpp
module A:P1;
S foo()
{
return { 1, 2 };
}
// file AP2.cpp
module A:P2;
S bar()
{
return { 41, 42 };
}
// file main.cpp
import A;
int main()
{
foo();
bar();
}The resulting program compiles and links fine.
What puzzles me is, when I look at the wording in the standard, it seems to me like this is not covered.
What's particularly interesting is, that it seems like the declarations in AP1.ixx are implicitly imported in AP1.cpp without importing anything (same for AP2.cpp).
For regular modules, this behavior is expected, but I can't seem to find wording for that behavior for partitions. It's like there would be something like an implementation unit for partitions.
I like what the MSVC compiler seems to be doing there. But is this covered by the standard?
If I use that, is it perhaps "off-standard"? What am I missing?
To my understanding, the following would be compliant with the wording of the standard:
// file AP1.cpp
module A;
S foo()
{
return { 1, 2 };
}
// file AP2.cpp
module A;
S bar()
{
return { 41, 42 };
}But then, a change in the interface of module A would cause a recompilation of both AP1.cpp and AP2.cpp.
With the original code, if I change AP1.ixx, AP2.cpp is not recompiled. This is great, but is this really covered by the standard?
Edit: The compiler is "Version 19.51.36122 for x64 (PREVIEW)"
14
u/STL MSVC STL Dev Mar 16 '26
FYI, the MSVC Compiler is now decoupled from the VS IDE, and we're shipping new stable compilers every 6 months, so if you're looking at the Latest build tools supported for production use, you're missing out on potentially half a year of compiler fixes. In contrast, if you select the Preview build tools, the latency for receiving compiler fixes will be reduced (we expect the latency to be ~1 week although it's a bit more at the moment). See https://devblogs.microsoft.com/cppblog/microsoft-c-msvc-build-tools-v14-51-preview-released-how-to-opt-in/ for instructions.
(Right now, the stable VS IDE and the VS Insiders IDE both offer Preview build tools, but confusingly, the stable IDE offers an older Preview than the Insiders IDE. I recommend Insiders IDE + Preview build tools if you want to check the most updated behavior of our current development sources. And yes, I am entirely aware that this is a dumpster fire orbiting a supernova of a versioning story.)
No idea if this affects your specific issue here, just general guidance since modules are receiving a large number of fixes over time.
3
u/tartaruga232 MSVC user, r/cpp_modules Mar 18 '26 edited Mar 18 '26
Thanks to all commenters and readers for their time!
So the consensus seems to be, that this behavior of the MSVC compiler is not compliant with the current C++ standard and other compilers don't support that behavior, but the behavior has the benefit that, for example AP2.cpp is not recompiled when only AP1.ixx is changed.
The MSVC compiler considers
to be an "implementation partition unit" of
like it would do for normal module interfaces and implementations. MSVC implicitly imports AP1.ixx in AP1.cpp.
The standard forbids this by saying there may be only one partition with the same name in a module.
Perhaps I'll document this later in a blog posting.
2
u/sudgy Mar 17 '26
I myself have tried this and it doesn't compile on GCC. It's both not standard and not portable.
13
u/not_a_novel_account cmake dev Mar 17 '26 edited Mar 17 '26
Yes, the default MSVC behavior is non-standard. You need to use
/internalPartitionwhen building partition implementation units to get standards conforming behavior.However your code is also non-conformant:
and
This is an MSVC extension. The standard does not allow multiple partitions with the same name.
The MSVC extension allows you to create an implementation partition and an interface partition of the same name, with the implementation partition implicitly importing the interface unit of its own name. That's where you're getting
Sfrom inAP1.cpp.This non-standard-by-default behavior has caused infinite confusion, in regular developers but also in build system and toolchain people who spend their 9-5s working on it (/u/mathstuf lost like a week trying to understand what was going on here). I wish they would not have done this as a default.
About once every month or two someone independently rediscovers MSVC does this to their great confusion. We keep a running list of "bamboozled by the MSVC modules extension". Welcome to the club.
1
u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Mar 20 '26 edited Mar 20 '26
The fact that the program in the first example compiles and links is just a manifestation of undefined behaviour.
You have a total of 7 translation units:
export module A;export module A:partition-name; which are correctly imported and then re-exported through the PMIU. Details heremodule A:partition-name; Details hereThe latter bullet point is where you have a special form of UB: ill-formed, no diagnostic required. The five module partitions must have five distinct partition names, but you give only three. Details also here. The fact that your
module Arefers nowhere to those ill-formed partitions saves you to some degree.I am aware that someone came up with the highly misleading characterization of non-exported module partitions as 'module implementation partitions', that you possibly fell prey of.
1
u/manni66 Mar 17 '26
https://en.cppreference.com/w/cpp/language/modules.html:
Your link:
You have:
and
That's wong. You use :P1 in two module units.