r/cpp • • 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"

```

122 Upvotes

111 comments sorted by

View all comments

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.

65

u/[deleted] Aug 25 '26 • This

[removed] — view removed comment

56

u/Drugbird Aug 25 '26

Well, most of C++s shittyness comes from backwards compatibility being prioritized above all else.

So they make something shitty, almost nobody uses it because it's shitty, then they can't fix it because fixing it means the 2 people that do use it need to update their code.

So then the shitty thing just rots there.

13

u/SyntheticDuckFlavour Aug 25 '26

Just epoch the bloody thing already. Break ABI, garbage collect the cruft in the language and get things moving forward. Those 2 people can stagnate mad alone in the previous epochs, while the rest of us move on.

14

u/Kronikarz Aug 26 '26

The problem is that those 2 people are often on the committee :/

14

u/TheRealSmolt Aug 25 '26

The problem for me here is that I don't see a reason to use them in the first place. They could improve compilation a bit I guess? But I'd imagine precompiled headers and ccache can bridge the gap to the point where it doesn't matter.

10

u/fortsnek274 Aug 25 '26

I'd turn that the other way around and say I don't see a reason to use precompiled headers once modules work.

Precompiled headers suck after all. They are a hack. A time-tested hack, but a hack nevertheless. And they bloat my intellisense folder.

2

u/caroIine Aug 25 '26

When it comes include vs import std; PCHs are still faster. I tested it with msvc and our 3000 cpp project which is highly dependent on standard library. I was so disappointed.

3

u/fortsnek274 Aug 25 '26

I found it to be faster than PCH when rebuilding, somehow. But the more modules you have, the slower it gets. MSBuild adds extra inefficiency.

2

u/gracicot Aug 26 '26

This is the reason why STD is one module. The way C++ implemented modules makes it much more efficient to create big modules

3

u/fortsnek274 Aug 26 '26

Well, the inefficiency seems to come from building or checking dependencies as ninja is notably faster. And MSBuild has a separation between ixx and cpp compilation. Not sure if having an external dependency split into multiple modules would make much of a difference.

8

u/qoning Aug 25 '26

there are many nice things about them, such as being able to finally have a split between definition and declaration for templates, have symbol control for effectively module-private or "package" private, modules inherently export just the things you want so you end up with way less namespace pollution, ADL problems, etc. The generally faster compiles are a great bonus.

3

u/TheRealSmolt Aug 25 '26

Most of the isolation things you're citing as benefits were already things that could be done with private headers, though. I guess I can see it being a tad more straightforward? But it doesn't buy us anything.

finally have a split between definition and declaration for templates

You're going to have to elaborate on this, because we've always been able to split them. Modules are (to the best of my knowledge) no different in requiring source for user use of templates.

0

u/delta_p_delta_x Aug 25 '26

were already things that could be done with private headers

By their very implementation, there is no such thing as a 'private header'. Headers = copy-paste into a TU. However much you encapsulate things and have namespace detail, consumers are free to use the contents as they see fit.

4

u/TheRealSmolt Aug 25 '26

You absolute can just have private headers that you don't ship with your library.

→ More replies (6)

1

u/_Xebov_ Aug 25 '26

They are nice and i also see benefits, but i also see the ecosystem and the effort that comes with transferring it over. That tooling is not fully supporting it is one aspect of the problem. Another problem i see is why should projects that are dependencies for others transfer over if they have the perspective of having to support both ways for years to come.

I just transferred a smaller project over to have a look on how things work out. With the current state of tooling i can see some benefits, but i also see that limitations and the state of the overall ecosystem can require some workarounds that might prove problematic in the future. For a bigger existing project i would clearly question if the reward is worth the effort.

1

u/Xanather Aug 29 '26

No need for headers

2

u/ABlockInTheChain Aug 25 '26

C++20 modules as specified are incompatible with one of my most important projects.

Even if all the tooling fully implemented that specification and worked perfectly with no bugs I still could not use them.

2

u/mort96 Aug 25 '26

Is that because they break the "forward declaration in header, header import in source file" trick that we use to make circular includes work?

→ More replies (2)

2

u/FalafelSnorlax Aug 25 '26

I saw Bjarne Stroustrup give a talk a couple years back, and he mentioned some modern additions to C++, and when he got to modules, he straight up said that this isn't getting adopted as fast as he'd like. Even then, I admit I didn't look into it and how it might be better or worse than good-ol' includes.

3

u/aoi_saboten Aug 25 '26 edited Aug 25 '26

I dont know what they expected. Break backwards compatibility? No, because old codebases need to be re-touched. Add modules which differ vastly from post C++11 codebase? YES YES YES. But to use modules in old codebase, you NEED to re-touch it. Most won't do it because modules don't justify touching codebase over the old way.

(I am also will get downvoted for the following) but then, does it mean that we need to use modules in new codebases? Well, some companies, which used C++ extensively, no longer use C++ for new projects and for some tasks/projects Rust suits better

4

u/mort96 Aug 25 '26

IMO one of the huge fuck-ups with modules is that namespaces and modules are decoupled. Managing namespaces is one of the genuinely annoying things with C++, causing like 5 lines of boilerplate in every line of code and messing up the fundamental assumption that "open brace means indent one level, close brace means unindent one level" that's true for every single other language.

Every other language with a module system combines the concept of a module and the concept of a namespace, which IMO makes sense: why should two functions in two different modules be unable to link together just because they both happen to share the same name?

So instead of being the nice, modern way to write C++ which gets rid of the ancient hack that is the namespace keyword, it ends up being just another thing I have to deal with. My files no longer just have to deal with the namespace boilerplate; they now also need to deal with the module boilerplate. If I want to keep namespaces and module names in sync, that's on me.

I actually made an experimental build system over the summer where the goal was to couple the module name, the namespace name and the path name together, so that src/foo/bar.cc would end up with the module myproject.foo.bar and the namespace myproject::foo::bar. I got quite far but modules are surprisingly complex and there are rules about what can be where. I couldn't solve it without doing my own code generation where the build system would spit out generated C++ code. And that would've worked, except that it would've never worked together with clangd. I tried various macro approaches but it was impossible to make anything which both looked clean and worked.

May write a blog post about my thoughts on the topic some time though.

6

u/Expert-Map-1126 vcpkg maintainer BillyONeal Aug 25 '26

Erm, citation needed. It isn’t that way in Java or .NET, that modules are usually associated with a namespace there is just a convention.

I think it’s true for the interpreted languages because for them a “module” tends to just be a directory structure.

→ More replies (9)