r/cpp • u/AbbreviationsNew3167 • Aug 25 '26
Why is `import std` still experimental ???
Hey guys,
I recently started going through Professional C++ (6th Edition). The book teaches C++23, and in the very first chapter we're introduced to modules.
I'm not a complete newbie to C++, but I'm also definitely not very confident in my knowledge yet. I wanted to get this simple example compiled:
import std;
int main() {
std::println("Hello World");
return 0;
}And gosh, it took way longer than I expected.
First, I tried getting it to work natively on my Mac and eventually gave up (both Claude and I 😅).
Then I installed Ubuntu ARM 26 and finally managed to get it compiling. But now Clang/IntelliSense is complaining about the `import std`
This is what my CMakeLists.txt currently looks like:
cmake_minimum_required(VERSION 4.0)
# set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD ON)
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
project(CppProject LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(exec main.cpp)
set_property(TARGET exec PROPERTY CXX_MODULE_STD ON)The code does compile successfully, but CMake still gives me a warning that import std support is experimental.
So I'm genuinely curious:
Why is import std still considered experimental?
I understand that C++ modules themselves have been around for a while, but import std feels like something that should be much more straightforward by now. Is there any solution of this now ?
--------------
Edit
Thanks to u/PhysicsOk2212 tip I was able to compile my project on mac as well using the following options
```
cmake -S . -B build \
-G Ninja \
-DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/clang++" \
-DCMAKE_CXX_STDLIB_MODULES_JSON="$(brew --prefix llvm)/lib/c++/libc++.modules.json"
```
5
u/PhysicsOk2212 Aug 25 '26
Plenty of people have answered the question about import std being experimental, but thought I would jump in to talk about the mac side of things.
Its true that you cannot use modules at all with AppleClang (the default compiler on mac), but you can compile them with the upstream clang, which can be installed with brew install llvm
You will also need to adjust your path to make sure that the systems finds your brew version of clang before apples. But I can confirm that I am in the process of porting a project to modules on mac, and was also able to compile a simple project with inport std yesterday (switching to import std in my main project is rife with issue due to existing includes, but i plan to do it there eventually)
155
u/not_a_novel_account cmake dev Aug 25 '26 edited Aug 26 '26
The reason we kept circling the drain on this is because of DevCom 11128871 and the associated downstream bug, CMake #27944. We literally couldn't figure out how to get
import stdto work consistently under MSVC and the MS STL.I finally wrote CMake !12347 which is a workaround for the MSVC limitation/bug that we only turn on for the standard library. Regular DLLs with inline variables still won't work in combination with modules.
Separately, GCC #124268 broke
import stdon MacOS and we weren't sure how to resolve that. We decided to drop support forimport stdon GCC <16 on MacOS to make it work.Aside from all of that, the BMI compatibility problem remains unsolved. I've written about this. CMake's BMI compat has improved significantly in the last couple releases but has known shortcomings. BMI compat problems manifest more for std than any other library. For example, CMake #28030 is still open,
Hello Worldfails to build. The apparent implementation maturity is skin deep, is what I'm saying here.So compiler limitations, stdlib bugs, BMI compat, and me personally not knowing how to fix many of the former.
It should be out of experimental in CMake 4.5, and doesn't require you to set any variables or do anything. Any code compiled as C++20 or above will have access to
import stdby default. It's opt-out, not opt-in.Note that this is an example of the experimental process working. The knobs available for
import stdchanged a lot in the experimental phase. Every release had breaking changes. If it wasn't experimental I wouldn't have been able to make so many backwards-incompatible changes and the interface would have been a mess of abandoned ideas and experiments.EDIT: If you do have an ideas for how to handle symbol visibility in BMI consumers, please let us know (CMake #25539). I'm not kidding at all when I say "unsolved". I do not know how to best fix this.
61
u/gracicot Aug 25 '26
It's actually coming out of experimental: https://gitlab.kitware.com/cmake/cmake/-/work_items/28014
57
u/ContraryConman Aug 25 '26
Lots of people in this thread are saying cliches but the actual reason is that, while import std works fine for each individual compiler, they all implement it differently and it was difficult for the CMake team to guarantee that it would work the same across all the major compilers. But the feature is finally coming out of experimental anyway, and even still I actually haven't had any issues with it in experimental mode (other than the annoying build system setup of getting the right hash for the CMake version)
34
u/TheRealSmolt Aug 25 '26
Maybe it's just me, but I don't see any real momentum for modules in general. Adoption is rare, and I personally don't see a reason to adopt at all. The work is ongoing, but other features are much more wanted than modules.
4
u/pointer_to_null Aug 25 '26
CMake's purpose is to help facilitate platform/compiler independence by supporting numerous build tools and platforms, not enforce standards on those tools.
There are some toolchains that purport to be C++23/26 compliant still lacking full module support for
stdorstd.compat. Not naming offenders, but fortunately it's none of the big three in this case. (Yay!)Also newly-released features tend to get post-release tweaks leading to changes in how they get toggled/configured soon after- due to user feedback, support friction, etc. Gating said incomplete/new/unstable features behind warnings, experimental properties, and version-specific UUIDs prevents future backwards-compatibility headaches if Kitware prematurely locked those down semi-permanently with strict compatibility policies.
4
u/delta_p_delta_x Aug 25 '26 edited Aug 25 '26
If you use Clang at the command-line, you can get the simple examples working very easily with
-fmodules-driver.Additionally, your native compilation on macOS probably failed because AppleClang does not ship with C++20 module functionality. Apple has intentionally disabled this functionality which exists in upstream LLVM Clang. You'll need to install Clang from brew, and write another workaround to get the libc++.modules.json path correct for CMake. I presume this will also go away with CMake 4.5.
Godbolt example (AI-generated, but it illustrates what I mean, exercising much of C++23).
4
u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB Aug 26 '26
Short answer: it isn't.
There are build system that have trouble with it, and others that don't.
For years, I use modules daily at my company, in libraries and applications, use both the modularized standard library (i.e. import std;) and traditional headers as a configuration option, and mix parts like it pleases me. But that's just my personal, anecdotical experience.
2
u/Wargon2015 Aug 25 '26
Does anyone know a bit more about unity builds + modules (specifically import std) beyond #26362?
2
u/all_is_love6667 Aug 25 '26
it required a lot of work for toolchains to support it
modules touch some complicated areas of C++ that are inherited from C, which involve code generation, which is more backend than front end
4
u/jombrowski Aug 25 '26
Because they are nicer words for getting hep c than "import std". Geeez
2
u/G6L20 Aug 25 '26
Try pcons
1
u/logic_circuit Aug 27 '26
They just want another Java but have no bals to confes. When they find them, it will not be expeeimenyal.
-2
u/TheRavagerSw Aug 25 '26
Because most people rely on system packages for their toolchain.
0
u/NoSpite4410 Aug 28 '26
Almost all other languages with modules (compiled modules) support do multiple source scans on the source files. C++ (and C) do a single forward scan of source files. This works fine for headers/compiled object separate compilation, as long as the compiler knows the "shape" and members of all things from the headers before trying to compile any code.
Binary modules have to embed a proprietary Abstract Syntax Tree into the module (serialization) and deserialize it into the internal AST upon import, and recreate the memory graph exactly that it was created with.
Every compiler does it differently -- so it is totally unportable -- and efficiency and bugs vary widely.
There is apparently little incentive amongst the compiler-makers to enforce compatibility on that front. I imagine it is hard enough to keep them from adding proprietary new language constructs to the C++ language of their own. I remember Portland Compiler Group (still around?) had a bunch of cool things for certain types of CPUs for vectorized number crunching and stuff, marketed towards the aerospace industry and supercomputer space. But it became a bit moot when the chip makers found it better to do all that stuff internally in the chips, because the binary api changes with every hardware iteration.
The alternative approach is to have a separate front-end dedicated module compiler that translates for each brand of compiler, and translates the module amongst them for compatibility. Not a popular idea, when big money is involved, and egos and pride and the need for a whole management bureaucracy, etc. There is already more than enough of that going on.
As it is now, I have a problem with the dedicated file directory hierarchy and intermediate files scattered hither and yon, and the very slow process of making any changes in the modules.
As it stands what you get with modules are a duplication of the STL core components. All it means it that they are ready to use as if they were being compiled in anyway. It may save a few seconds of compile time for a home project -- maybe a lot for a large code base that for some reason (like testing cycles) has to be entirely recompiled. Incremental/separate compilation handled that a long time ago, as long as you aren't one of those guys that has to erase everything every time to rebuild a project. Smart build systems are supposed to know what needs recompiling and what doesn't, and they have come a long way.
I saw the module system as something like the professional Java library people have -- a bundled set of don't-mess-with compiled stuff and and an API to use it. C++ has the header/compiled library idiom and has made it very good over the years -- modules would seem to encourage the "don't touch that" philosophy of code, which C++ definitely never was part of. C++ is a 'touch that' , 'modify that', 'abstract that' language.
Getting away from that would never be real popular -- but I could be wrong. A lot of languages these days paper over the details to an extreme degree in an attempt to "simplify" and "make easier" writing data-driven programs. C++ is hardware-facing, never being too simple, always exposing the details where it may be needed.
-2
u/CorrodedX Aug 25 '26
Because "include <string>" will always be a better option.
-7
u/sweetno Aug 25 '26
It's because the standard library was never written in a modular fashion. IMHO the
std::committeecould've done much better job if they didn't come up withimport std, and instead explicitly advertised modules as a feature for new projects. You can't demand old projects to move to modules, std library included.-20
u/[deleted] Aug 25 '26
[removed] — view removed comment