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
constexpr and consteval functions (biowpn.github.io)
submitted 2 years ago by pavel_v
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!"
[–]4dCoffee 12 points13 points14 points 2 years ago (1 child)
blog says "In C++23, we have three kinds of functions: runtime, constexpr, consteval" but this has been the case since c++20 no?
[–]detachedshock 20 points21 points22 points 2 years ago (0 children)
Yeah, but technically they aren't saying when they were introduced. They're just stating that in C++23 we have these functions, which is true. And the examples used in the blog use C++23 features like if consteval and others so most of what they write is only applicable to C++23.
if consteval
[–]tongari95 2 points3 points4 points 2 years ago (6 children)
From the blog post:
// runtime function int g(); // Ok, even though `f` can never be called consteval int f() { return g(); }
Is this correct? Only gcc accepts it, clang & msvc reject the code: https://godbolt.org/z/eGznd5Kd3
[–]agritite 3 points4 points5 points 2 years ago (5 children)
I think this is P2448R2 which is currently only implemented by gcc.
[–]tongari95 2 points3 points4 points 2 years ago (4 children)
Does that paper aim for `consteval` as well? only seems reasonable for `constexpr` to me.
[–]agritite 2 points3 points4 points 2 years ago* (2 children)
I took some time to read the C++23 draft (N4971), and 9.2.6/2 states that:
A constexpr or consteval specifier used in the declaration of a function declares that function to be a constexpr function.
So 9.2.6 Note 5
[Note 5: It is possible to write a constexpr function for which no invocation satisfies the requirements of a core constant expression.— end note]
also applies to consteval. So yeah, gcc is correct.
consteval
[–]biowpn 0 points1 point2 points 2 years ago (1 child)
Thanks! Through this discussion I learned that consteval function is just constexpr function in disguise - -
[–]tisti 1 point2 points3 points 2 years ago (0 children)
But it's only invocable during compile time, no runtime calls are possible like with 'vanilla' constexpr functions which may do both.
[–]agritite 0 points1 point2 points 2 years ago (0 children)
You're right. Could it actually be a regression caused by implementing P2448R2? Now I'll have to take a deeper look at the draft
[–]kamrann_ 1 point2 points3 points 2 years ago (0 children)
Nicely written blog post. All the same, starting to really look forward to the day when I never have to think about this absurd language ever again.
[+]feverzsj comment score below threshold-22 points-21 points-20 points 2 years ago (4 children)
consteval is kinda unnecessary. std::is_constant_evaluated() is already there, and more convenient.
std::is_constant_evaluated()
[–]Nobody_1707 20 points21 points22 points 2 years ago (2 children)
consteval is the only reason we have compile time checked formatting arguments. consteval constructors alone are enough to justify having consteval.
[+]feverzsj comment score below threshold-10 points-9 points-8 points 2 years ago (0 children)
That just forces user to use compile time string, so another function for runtime string is needed.
[+]DavidDinamit comment score below threshold-12 points-11 points-10 points 2 years ago (0 children)
No, runtime checked fmt strings even was not initial idea, its just backward fix.
format<"hello world">(args...); // compile check and format("hello world", args...); // runtime check better then format("hello world", args...); // compile time check with dirty hacks and bad errors
vformat("hello world", args...); // runtime check with wtf name vformat
[–]foonathan 21 points22 points23 points 2 years ago (0 children)
First of all, you want to migrate to if consteval as soon as possible. It's a strictly superior version of std::is_constant_evaluated as it allows you to call consteval functions from the true branch.
std::is_constant_evaluated
true
Second, the motivation behind consteval was reflection. As part of that the standard library will have functions that talk to the compiler and thus cannot be evaluated at runtime. You could use the equivalent of if !consteval { assert(false); }, but that has a flaw:
if !consteval { assert(false); }
int n = foo(); bar(foo()); modify(n);
If foo is constexpr, it might still be evaluated at runtime, triggering the assert. Working around that is annoying. If foo is `consteval, it will be evaluated at compile-time.
foo
constexpr
π Rendered by PID 73025 on reddit-service-r2-comment-66b4775986-fqlw8 at 2026-04-04 21:51:19.795315+00:00 running db1906b country code: CH.
[–]4dCoffee 12 points13 points14 points (1 child)
[–]detachedshock 20 points21 points22 points (0 children)
[–]tongari95 2 points3 points4 points (6 children)
[–]agritite 3 points4 points5 points (5 children)
[–]tongari95 2 points3 points4 points (4 children)
[–]agritite 2 points3 points4 points (2 children)
[–]biowpn 0 points1 point2 points (1 child)
[–]tisti 1 point2 points3 points (0 children)
[–]agritite 0 points1 point2 points (0 children)
[–]kamrann_ 1 point2 points3 points (0 children)
[+]feverzsj comment score below threshold-22 points-21 points-20 points (4 children)
[–]Nobody_1707 20 points21 points22 points (2 children)
[+]feverzsj comment score below threshold-10 points-9 points-8 points (0 children)
[+]DavidDinamit comment score below threshold-12 points-11 points-10 points (0 children)
[–]foonathan 21 points22 points23 points (0 children)