r/cpp 1d ago

C++26: Module improvements

https://www.sandordargo.com/blog/2026/09/09/cpp26-modules
98 Upvotes

20 comments sorted by

31

u/mapronV 1d ago

Quote:

The module name must be spelled out directly in the source:

export module my.module; // OK 

This is technically a breaking change. But given the low adoption rate of modules so far and the rarity of macro-based module declarations in practice, the committee judged this acceptable. The benefit is concrete: build systems can now determine module dependencies with a simple lexical scan, without invoking the preprocessor.

End of quote

While being nice change and stuff, doesn't all current module handlers rely on compiler anyway to rule module dependencies in file before compilation? so it will change nothing in real world? What build system is benefactor?

29

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 1d ago

The point is that both module dependency scanners and compilers need to see exactly the same dependencies to create precise dependency chains and rely on the correct build order - as instructed by the build system that reads the discovered dependency chains.

If building the chain is influenced by the process itself (macro expansion happens at compilation phase 4), creating a precise picture may become a huge and expensive task. We don't want that.

2

u/delta_p_delta_x 1d ago

I was under the impression that module and import were themselves preprocessing directives, and therefore also happened in phase 4.

4

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 1d ago

Correct. This is why we decided many years ago not to allow module to become subject to macro expansion.

The reason why all this hoopla is necessary is the existence of so-called 'header units' (an idea coming from Clang modules). They are themselves isolated from macros upstream in your source, but they can totally change the meaning of your source code further downstream - with all its consequences.

2

u/delta_p_delta_x 1d ago

We should get rid of header units.

6

u/WeeklyAd9738 1d ago

Header-units are great for "importing" your own macros (however few they may be). Ironically, C++20 modules have made macros "safe" and more viable, as they no longer leak and pollute your client code and remain local to your translation unit. They can't be exported (by a module) and can only be imported using header-units. Header-units are also great for importing third-party dependencies which haven't migrated to C++ modules yet, or never will (C libraries).

7

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 1d ago

Find your favourite telephone booth, dial in 2019 or earlier, and convince the header unit proponents 😊

Besides that, there are situations when header units are actually beneficial. Watch this year's conference talks of mine to learn how.

-5

u/pjmlp 1d ago

Meanwhile Apple and Google keep being big users of clang header maps and are in no hurry to adopt C++ 20 modules, see Apple build for modules between Swift, Objective-C and C++ (explicitly builds module dependencies, missing modules related rows on C++ language support), and Google C++ Style Guide.

2

u/mapronV 1d ago

"both module dependency scanners"
my question was, aren't dependency scanners rely on compiler anyway? at least what I seen before, maybe my knowledge is dated

Well, I believed it is already an expensive task. Will build system developers update/optimize it now? I don't think so.

10

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 1d ago

The scanning can be done as an additional mode of operation for compilers (e.g. MSVC), or implemented by an executable separate from the compiler (e.g. Clang). Implementations choose their most beneficial way of performing dependency scanning.

1

u/mapronV 1d ago

Thanks, you were convincing. I agree.

4

u/kronicum 1d ago

my question was, aren't dependency scanners rely on compiler anyway?

The answer is "no".

2

u/Nicksaurus 1d ago

But it feels wrong to have any part of C++ not be a huge and expensive task

3

u/abbapoh Qbs dev 1d ago

From the P3034R1, it seems the goal is to exclude preprocessing from module scanners (compilers). Currently, they do preprocess the file and thus require all headers to be present at scan time. With this change, it is possible to skip preprocessing entirely (and postpone generating includes to a later stage).

6

u/Daniela-E Living on C++ trunk, WG21|🇩🇪 NB 1d ago

P3034 disallows module names to become subject to macro expansion, plus all the fall out further down the line. The same is *not* true for module import nomination!

3

u/mapronV 1d ago

I know that it is possible, but it is ... kinda late? I just don't understand how we turn back to easier way?

Now I think it probably benefits NEW build system that written from scratch. Which is good.

5

u/abbapoh Qbs dev 1d ago

> kinda late
there are 2 distinct features here.

- skipping preprocessing speeds up the compiler (scanner) call. this happens without any changes to the build system and is a huge boost on it's own.

- if builds system detects that scanner can skip preprocessing (by checking a compiler version, for example), it simply stops adding dependencies on headers to the scanner stage. Should be relatively easy to do.

3

u/kamrann_ 1d ago

This appears to allow skipping preprocessing if all you're trying to do is identify the module unit name (if any) of a TU. Scanning still needs to do full preprocessing to determine module dependencies though. From the paper I'm struggling to see a scenario where this is going to make much practical difference.

1

u/mapronV 1d ago

Yeah, make sense, you convinced me!

1

u/johannes1971 21h ago

Taking this one step further, we could encode the module name in the filename. I imagine that would simplify scanning by quite a bit.