r/cpp • u/Recent-Dance-8075 • May 03 '26
Migrating a small C++ code base to C++26 (modules, import std and contracts)
https://jonastoth.github.io/posts/migrate_cxx26/I wrote a blog post about my experience of converting a small personal toy project to the latest C++ features, compiling with gcc-16 and llvm-22 (the whole ecosystem of each compiler).
Setting up tooling and fixing issues took most of the time, so I documented the necessary steps.
Even though the target code base is not serious, the experience on the loopholes to jump through are hopefully valuable for others trying something similar.
Additionally, this is Feedback for the tool developers about existing rough edges.
https://jonastoth.github.io/posts/migrate_cxx26/
No AI was involved in the creation of the post nor in the creation of the software project. If I violated any rule or the post is not eligible for this sub, please remove it.
9
u/jwakely libstdc++ tamer, LWG chair May 03 '26
That's not true, I don't know what cmake does to suggest that.
Why use this to check for missing Contracts support, instead of the standardized macro for checking exactly that feature?
7
u/Fit-Departure-8426 May 03 '26
Good writeup! But Im not sure why you would want using namespace std? And how is that related to « modern » c++?
6
u/simplex5d May 03 '26
As a toolsmith myself (ex-SCons maintainer and author of pcons), perhaps you'd like to try pcons for your build? It's an easy port from cmake and should simplify things significantly for you. pcons fully supports C++20 modules so I'd be greatly interested in any feedback. https://github.com/DarkStarSystems/pcons or https://pcons.readthedocs.io.
13
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting May 03 '26
😔
3
u/ChuanqiXu9 May 06 '26 edited May 06 '26
For measuring speed, maybe it is better to exclude the time to build std module itself. As it is slow and it won't change frequently.
1
u/Recent-Dance-8075 May 03 '26
Thanks for all your feedback. I updated the blog post and referenced your suggestions and comments!
3
u/RadimPolasek May 03 '26
That was a really pleasant read 👍
2
u/ABlockInTheChain May 04 '26
Not to mention how easy it is to write an ebuild and make your own projects first class system packages.
It's a shame Gentoo is not more well known. It really should be considered the ideal Linux distribution for software development.
1
u/Tall-Assignment1349 Jul 27 '26
Late to this, but great writeup. Small plug with disclosure: I build byteask (an AI agent for C/C++ that grounds answers in the actual ISO docs/compiler manuals instead of guessing) and i worked recently on the __cpp_contracts vs #ifdef __clang__ mix-up jwakely. I had to make sure it cites feature-test macro table rather than pattern-matching. If you end up doing the incremental-build measurements you described, would love to hear how the compilers compare.
0
u/javascript May 03 '26
My understanding is that umbrella imports like `import std;` can reduce build parallelism. If you have multiple translation units, it can be beneficial to reduce your module imports to be more granular. This is good for IWYU/IWYS cleanliness but it also helps improve compile times.