Visual Studio 2022 17.5 and later supports importing the C++23 standard library named module std
by replacing all instances of #include <header>
with import std;
where <header>
is a standard library header.
According to the tutorial there are some limitations, such as the following:
Don't mix and match importing C++ standard library header files and named modules. For example, don't #include <vector> and import std; in the same file.
Does this mean that if I use import std;
in a file (by "file" I assume that the tutorial is referring to a .cpp
file or other translation unit), then none of the non-standard library headers that it #include
s is allowed to transitively #include
a standard library header?
I'm pretty sure the answer is obviously yes, because after preprocessing, it doesn't really matter whether an #include
is transitive or not - its contents will end up in the .cpp
file mixed with the import
statement.
But then what can be done to prevent mixing besides an exhaustive search of all transitive #include
s to see if there are any standard library headers? Especially if a project depends on something like Boost, it doesn't seem that import std;
will be a realistic option until Boost is itself available as a module, right?
So in the meantime, am I right that import std;
is basically only realistic for new or small projects where the standard library header dependencies (both direct and transitive) can be fully tracked and migrated at the same time? Or is there a way to somehow migrate a project to import std;
even if some of its dependencies have not yet migrated?
If I compile the following code with Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32824 for x64 then I get C2572, which indeed suggests that transitive #include
s cannot be mixed with import std;
.
header.hpp
#include <iostream>
main.cpp
#include "header.hpp"
import std;
int main() {
std::cout << "Hello, world!\n";
}
C:\Code\import-std-test>cl /std:c++latest /EHsc /nologo /W4 /MTd "%VCToolsInstallDir%\modules\std.ixx" main.cpp
std.ixx
main.cpp
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\xtr1common(42): error C2572: 'std::enable_if': redefinition of default argument: parameter 1
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.37.32822\include\xtr1common(43): note: see declaration of 'std::enable_if'
main.cpp(5): fatal error C1117: unrecoverable error importing module 'std': symbol 'byte' has already been defined
Generating Code...
Thanks to @BoP's comment it's now clear that mixing import std;
and #include <header>
is supposed to work but simply hasn't been implemented yet in MSVC (a fact that unfortunately is not mentioned in the tutorial). The linked talk doesn't mention when it will be available, but it sounds like Microsoft is actively working on it.
So to answer the original question: no, import std;
is not a realistic goal at the moment for most projects, but yes, it will be when MSVC fully implements the standard, which allows for mixing the two.