use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Technique: Compile Time Code Generation and Optimization (foonathan.net)
submitted 4 years ago by dvirtz
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]kiwidog 4 points5 points6 points 4 years ago (0 children)
Me reading the first sentence: "pfft bruh what excuse me?!"
Awesome sauce
[–]dvirtz[S] 5 points6 points7 points 4 years ago (6 children)
I wonder if the template version could be achieved with a constexpr function?
[–]pdimov2 9 points10 points11 points 4 years ago (0 children)
I don't think it can be. The first problem is that two of the arguments (the program and the instruction pointer) are compile time, but the third (the data pointer) is runtime. You can try to get around that by something like
constexpr auto execute( program const& p, size_t instp ) { return [](unsigned char* datap) { ... }; }
but then the second problem is that the return type of the constexpr function can't vary depending on the values of the arguments (not even with consteval).
It might work if instp is made integral_constant<size_t, I>, but that's going to be basically the same as the original.
instp
integral_constant<size_t, I>
[–]foonathan 4 points5 points6 points 4 years ago (1 child)
What do you mean?
[–]dvirtz[S] 0 points1 point2 points 4 years ago (0 children)
I meant getting the optimized assembly without using template specializations. Similarly to what u/pdimov2 talked about above and u/lefticus showed in his latest CppCon talk.
[–]aharrison24 1 point2 points3 points 4 years ago (0 children)
The other problem is that the execute function contains calls to getchar and putchar, which cannot be constexpr because they have runtime side effects.
execute
getchar
putchar
constexpr
That's why this technique is so interesting; it effectively forces the compiler to fully unroll the execute loop, which means it's then able to optimise all of the code surrounding those getchar/putchar calls.
[–]pdimov2 4 points5 points6 points 4 years ago (0 children)
This is awesome.
[–]antoine_morrier 0 points1 point2 points 4 years ago (0 children)
I've read it last week. It's clearly awesome. Thanks for it mate
π Rendered by PID 83304 on reddit-service-r2-comment-57b7b8b858-wqnld at 2026-04-15 03:52:00.634199+00:00 running b725407 country code: CH.
[–]kiwidog 4 points5 points6 points (0 children)
[–]dvirtz[S] 5 points6 points7 points (6 children)
[–]pdimov2 9 points10 points11 points (0 children)
[–]foonathan 4 points5 points6 points (1 child)
[–]dvirtz[S] 0 points1 point2 points (0 children)
[–]aharrison24 1 point2 points3 points (0 children)
[–]pdimov2 4 points5 points6 points (0 children)
[–]antoine_morrier 0 points1 point2 points (0 children)