r/cpp • u/hellfoundry • Sep 22 '26
r/cpp • u/drop-volley • Sep 22 '26
Stateful compile-time functions
In section 9.1.2 of this book[1] it mentions that C++23 allows for stateful constexpr functions, and even shows an example. I can't seem to get it to compile on godbolt. Is the book completely wrong about something so fundamental, or am I doing something wrong?
[1] https://simplifycpp.org/books/cpp/Mastering_Modern_C++23_A_Complete_Guide_to_the_Latest_Standard.pdf
Edit: Godbolt link with code from that section -> https://godbolt.org/z/94j9EK87b
r/cpp • u/NicoJosuttis • Sep 20 '26
Printing UTF-8 strings since C++23
C++23 provides significant better support to print UTF-8 strings.
For example:
std::string s = "K\u00F6ln: 100\u20AC"; // string ö and €
std::println("{}", s); // prints "Köln 100€"
// print hexadecimal code units (bytes of the UTF-8 string):
std::vector<char> v{std::from_range, s};
std::println("{:n:X}", v); // prints 4B, C3, B6, 6C, 6E, 3A, 20, 31, 30, 30, E2, 82, AC
// new format specifier to interpret array of char as string:
std::println("{:?s}", v); // prints "Köln 100€" This even works fine on Windows platforms (using I/O streams or std::format() might print garbage with a native Windows compiler on a native Windows platform).
See sections 3.2.3 and 4.3.3 of www.cppstd23.com .
r/cpp • u/adityazero • Sep 20 '26
CppCon At cppcon WG21 had a whiteboard asking attendees what they would like to have in C++29
some of the ones I remember was:
- contracts
- profiles
- delete moar stuff
I'm curious what folks here expect. Also if someone remembers rest of the list, that'd be great.
r/cpp • u/13steinj • Sep 20 '26
CppCon The Address is Not The Place: Object Residency in C++26 - Laurie Kirk - CppCon 2026
youtube.comr/cpp • • Sep 19 '26
A clever branch free optimization
I'm the developer of memlz which is an extremely fast compression library.
We have char* src, char* dst for the source and destination buffers and uint16_t flags that we prepend bits to one at a time. The inner core looks something like this (pseudo code):
flags <<= 1;
uint64_t payload = ((uint64_t*)src)[i];
uint16_t hash_index = hash_function(payload);
if (hash_table[hash_index] == payload) {
flags |= 1;
*(uint16_t*)dst = (uint16_t)hash_index;
dst += sizeof(uint16_t);
}
else {
*(uint64_t*)dst = payload;
hash_table[hash_index] = payload;
dst += sizeof(uint64_t);
}Depending on a hash comparison we need to output either 2 or 8 bytes, advance dst accordingly and prepend either a 1 or a 0 to flags.
Furthermore, at the end it's required that hash_table[hash_index] == payload, so we update hash_table in the false-case.
Let's benchmark it with 211 MB input so it's uncached:
memlz: 2744 MB/s
Now let's look at an amazing idea. The output to *dst can be rewritten with the ternary operator:
*(uint64_t*)dst = hit ? hash_index : payload;
That turns into a compare instruction (cmp) and a conditional move instruction (cmove) with no branching:
00007FF742A5A55B mov rax,r10
00007FF742A5A55E cmp rcx,r10
...
00007FF742A5A56D cmove rax,rdx
00007FF742A5A571 mov qword ptr [rdi],raxThe advancement of dst is also simple to make branchless, noting that hit will turn either 0 or 1:
uint64_t hit = (hash_table[hash_index] == payload);
dst += 8 - (hit * 6);Now look at updating the hash_table where we update it to payload only if it differed from payload. What we can do is update it unconditionally at the end, and hope that the extra number of unnecessary writes to memory (where it just overwrites the same value) are not too expensive.
Final branch-free version:
uint64_t payload = ((uint64_t*)src)[i];
uint16_t hash_index = hash_function(payload);
flags <<= 1;
uint64_t hit = (hash_table[hash_index] == payload);
flags |= hit;
*(uint64_t*)dst = hit ? hash_index : payload;
hash_table[hash_index] = payload;
dst += 8 - (hit * 6);Let's benchmark it:
memlz: 4269 MB/s
I had never believed such gains were possible because I had been micro-optimizing on this for weeks.
I later achieved 5070 MB/s. To put it in perspective, uncached memcpy runs at 14000 MB/s here and LZ4 - another popular fast compression library - at 718 MB/s.
[EDIT: The unaligned memory reads and writes are UB. For the dst write can simply declare uint64_t hash_index and use memcpy() which the compiler optimizes into the same binary code]
[EDIT: 19 days after I'm at 6304 MB/s]
r/cpp • u/Expert_Sheepherder24 • Sep 19 '26
index based for vs for-each
godbolt.orgIt's my first post. I tested std::vector<char> old index based for vs modern for-each. Result is very interesting.
Clang-23.1.0 -O2 -std=c++20 -stdlib=libc++
Time: 0.073599 sec inc_vector
Time: 0.002597 sec inc_for_eachGCC 16.2 -std=c++20 -O2
Time: 0.077772 sec inc_vector
Time: 0.007880 sec inc_for_eachMSVC-v19.latest /std:c++20 /O2
Time: 0.058504 sec inc_vector
Time: 0.001825 sec inc_for_eachCode:
#include <chrono>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>
static std::vector<char> gen_random(size_t n)
{
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 255);
std::vector<char> vec(n);
for (size_t i = 0; i != n; i++)
vec[i] = dis(gen);
return vec;
}
int inc_vector(std::vector<char>& v) {
for (size_t i = 0; i < v.size(); i++){
v[i]++;
}
volatile int result = v[v.size()/2];
return result;
}
int inc_vector_for_each(std::vector<char>& v) {
for (char& e: v){
e++;
}
volatile int result = v[v.size()/2];
return result;
}
volatile int sink{}; // ensures a side effect
int main()
{
auto benchmark = [](auto fun, auto rem)
{
auto vec = gen_random(1'000'000);
const auto start = std::chrono::high_resolution_clock::now();
for (auto size{1ULL}; size != 100ULL; ++size)
sink = fun(vec);
const std::chrono::duration<double> diff =
std::chrono::high_resolution_clock::now() - start;
std::cout << "Time: " << std::fixed << std::setprecision(6) << diff.count()
<< " sec " << rem << std::endl;
};
benchmark(inc_vector, "inc_vector");
benchmark(inc_vector_for_each, "inc_for_each");
return 0;
}r/cpp • u/TheRavagerSw • Sep 20 '26
Self Contained Source Groups are amazing
I basically adopted this idea from Abseil (https://abseil.io/) and from this CppCon talk: https://youtu.be/re4Oy1IVj-s?si=3bwtKqOyMAX6WSAs.
I structure each source group in my projects like this:
src_grp/
├── iface/
│ └── smth.cppm
├── impl/
│ └── smth.cpp
├── inc_pub/
│ └── pub.hpp
├── inc_priv/
│ ├── priv.hpp
│ └── maybe.inc
├── test/
│ └── test.cpp
├── bench/
│ └── bench.cpp
└── docs/
├── some.md
└── doxygen.inI strongly recommend this approach over older project-wide conventions such as having a single global test/ or bench/ directory etc.
r/cpp • u/Salt-Sherbet-2950 • Sep 18 '26
Good Active Open Source Projects Looking For Contributions
Hi. I'm wondering if someone with knowledge of the open source community knows of any projects that I can contribute to using C++ . I am looking to put some real experience on my resume/help others out. Thanks and have a great day!
r/cpp • u/kindredseer • Sep 18 '26
Embedded C++
Just curious how useful the ability to embed C++ as a scripting language into your C++ programs would be? I mean like runtime JIT execution of C++ code in much the same way you might embed Lua, including the ability to restrict/sandbox it as necessary.
r/cpp • u/MartynAndJasper • Sep 18 '26
C++ Salary expectations
Mods: Please note that I am not looking for jobs or trying to break any rules for this subreddit!
This is more about advice...
I've been offered a senior role for maintaining a (few decade old) codebase in C++.
It's a complex product and I would be taken on as a senior software engineer. I have over three decades of experience, over 15 in C++ but more recently in c#.
We are now at the point where money is been discussed.
They are going to make me an offer.
This is a tricky one because I have not been applying for C++ roles for a while. I don't want to undersell myself but nor do I want to see myself undercut.
I'm of the consideration that a C++ role will command a higher salary than something like asp.net.
This is far more of a niche skill than it used to be and AI leads me to believe that it, for that reason, imply more money.
I know this has implications based on regionality and geography, but what kind of salary would you advise that I pitch for? For relevance, I'm UK based.
Any thoughts?
Bringing Correctly Rounded Math to Production with LLVM-libc
devblogs.microsoft.comThe first part of this series, "MSVC C++23: constexpr cmath with LLVM Libc", was discussed 6 days ago: https://www.reddit.com/r/cpp/comments/1wcx97o/msvc_c23_constexpr_cmath_with_llvm_libc/
This is the promised guest post written by LLVM-libc maintainers Michael Jones and Tue Ly at Google. I had no idea about the recent algorithmic advancements here!
(Before anyone asks: Yes, I am self-posting this series as it's published. That's a bit more self-posting than we generally like to see, but constexpr <cmath> is of wide community interest, and I hope I've built up enough goodwill by moderating this subreddit for years.)
r/cpp • u/madnirua • Sep 16 '26
Declarative GUI toolkit Slint 1.18 Released
slint.devwith new Flexbox layout, with spring animations, change the z-order at run time, an experimental Vello renderer and more.
r/cpp • u/AriyaSavaka • Sep 15 '26
free COMP4300 course - Intro to Game Programming in C++ (Sept 2026)
youtu.ber/cpp • u/petrdan • Sep 15 '26
From Templates to Reflection, Part 1: Back to the Basics with type_vector
daniel-petrovic.github.ior/cpp • u/rsjaffe • Sep 14 '26
Push-based vs Pull-based Customization
brevzin.github.ioEmulating Rust attributes with reflection (or at least trying to).
r/cpp • u/Remi_Coulom • Sep 14 '26
What’s New for C++ Developers in Visual Studio 2026 (18.7 – 18.10)
devblogs.microsoft.comr/cpp • u/ProgrammingArchive • Sep 14 '26
New C++ Conference Videos Released This Month - September 2026 (Updated To Include Videos Released 2026-09-07 - 2026-09-13)
C++Now
2026-09-07 - 2026-09-13
- Testing Everything in Constexpr - Braden Ganetsky - https://youtu.be/SgIiQS6RO60
- Plato, Magritte, Sartre & Carroll - Learning Software Design from the Masters - Ben Deane - https://youtu.be/7QZchipF2gU
- Functional Composition Beyond Monads - Jonathan Müller - https://youtu.be/DcUOZE5FHDc
2026-08-31 - 2026-09-06
- Incremental Modernization - Refactoring Legacy C++ OOP Application Code with Functional Programming and Type Erasure Concepts - Jessica Ding - https://youtu.be/FOzGlO5XaPE
- When Atomics Lose - Architecture‑Dependent Performance of C++ Synchronization - Sampad Acharya - https://youtu.be/Ehy9wutYYH8
- Multi-Priority Graph-Based Parallel Execution Using Taskflow - Yun Kai Peng - https://youtu.be/4I5jsK3zRv0
ACCU
2026-09-07 - 2026-09-13
- Taming C++23 - The Things About C++23 You Do Not Know - Nicolai M. Josuttis - https://youtu.be/IVee1JXbrQE
- Rust for The Curious C++ Developers - Victor Ciura - https://youtu.be/2hf0PHcpKBI
- O(1) or O(no-no-no) - Mastering the unordered_map - Kevin B. Carpenter - https://youtu.be/OF4Id8085s0
2026-08-31 - 2026-09-06
- Software Abstraction in the Age of AI-Generated Code: Languages, Compilers, and Systems Engineering - Andrei Alexandrescu - https://youtu.be/-RWdevA0gWI