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

0 Upvotes

10 comments sorted by

6

u/AKostur Sep 22 '26

Try r/cpp_questions

When you do, perhaps it’s a good idea to provide a link to your attempt in godbolt so that they’ll be able to see what you’re actually trying.

2

u/drop-volley Sep 22 '26

I added a Godbolt link. I figured this was the right subreddit because it's a language feature question, but I realize now that I was wrong. My bad.

2

u/FoxCanFly Sep 22 '26

How is it supposed to work? Imagine having this function defined in a header. Two translation units call it without knowing each other. How is the compiler expected to be aware of every translation unit previously called the function and in what order?

1

u/drop-volley Sep 22 '26

Good point I would be happy if it worked only in consteval functions within one translation unit, but statics don't work in consteval functions either.

15

u/ppppppla Sep 22 '26

I would advice to immediately delete this book from your drive. Under the sample from 9.1.2 there is an incredibly puzzling sentence:

thread_local variables allow compile-time state per thread in multithreaded constexpr evaluation.

Complete slop. Nothing of this statement makes any sense at all, and who knows how much bad information is in the rest of the book.

20

u/eisenwave WG21 Member Sep 22 '26

The book is completely wrong and probably AI slop. C++23 does allow static constexpr variables in constexpr functions, but not mutable static variables.

I guess whoever prompted this book fed a summary of new C++23 features into the AI and this is the incoherent slop that came out the other end. Anyone who actually knows what features made it into C++23 or who spends 10 seconds on compiling their code examples would have realized that this doesn't work.

12

u/PowerShartOffice Sep 22 '26

The author has a post on LinkedIn titled "How I Authored Over 200 Specialized Technical Books with the Help of Al (And Why I Decided to Publish Them for Free)"

10

u/MaitoSnoo [[indeterminate]] Sep 22 '26

if you want to learn C++, learn it using a reputable book, not a vibewritten ebook by some rando

2

u/13steinj Sep 23 '26

The example is completely nonsensical and likely LLM slop.

There are ways to acheive such a feat, via "stateful metaprogramming", commonly done via template-member-friend-injection (and in C++26, you can also do this using reflection).

1

u/ItsRSX Oct 05 '26

We used to have this before via not-managed for-MFC not-MFC mode MSVC __if_exists and other actually-somewhat-standard tricks to define monotonic counters. The biggest abuser of this being boosts typeof before decltype was a thing. iirc, herb sutter went out of his way to kill it everywhere possible around 2015. Good luck getting stateful machines with more than a few bits of entropy at compile time to actually work in <current year>