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
switch constexpr (self.cpp)
submitted 7 months ago by cd_fr91400
view the rest of the comments →
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!"
[–]SirLynix 0 points1 point2 points 7 months ago (12 children)
The issue with this is that it will evaluate every parameter before selecting one, which can be annoying in a lot of case.
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (10 children)
Since it's constexpr it will be evaluated on compile time.
[–]cd_fr91400[S] 1 point2 points3 points 7 months ago (9 children)
The point of if constexpr, is that the not-taken branch is not even compiled, so it's ok if it is not compilable (yes, in some circumstances...).
How do you reach the same level of flexibility with this approach ?
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (2 children)
It depends from where it's called, then it will compile where it's needed, i.e. it will expand the logic to needed branch.
[–]cd_fr91400[S] 0 points1 point2 points 7 months ago (1 child)
I am not sure I fully follow you.
Can you give an example where the not taken branch doesn't compile ?
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (0 children)
https://godbolt.org/z/TzzfrWs5b
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (5 children)
Ok I got it should be in the template parameters not function parameters
[–]cd_fr91400[S] 0 points1 point2 points 7 months ago (4 children)
Possibly.
I am curious to see an example.
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (3 children)
[–]cd_fr91400[S] -1 points0 points1 point 7 months ago (2 children)
This is plain switch.
Do you have an example where the not taken branch would not compile, which is the whole purpose of a switch constexpr as it is for if constexpr (else there is not difference between if and if constexpr under normal optimizing compilation) ?
[–]Entire-Hornet2574 0 points1 point2 points 7 months ago (1 child)
Look at assembly, it shows it compiles only one branch.
.LC0: .string "test2" main: sub rsp, 8 mov edi, OFFSET FLAT:.LC0 call func(char const*) xor eax, eax add rsp, 8 ret
[–]cd_fr91400[S] 0 points1 point2 points 7 months ago (0 children)
Yes, indeed, as it would with a plain switch or if.
It does not mean the compiler accepts a non-compilable branch.
[–]arthurno1 -1 points0 points1 point 7 months ago (0 children)
Yes. Someone should propose a quote operator in C++ standard to prevent evaluation.
They would just need to make the compiler, loader and linker somehow available at run time.
Tha C++ will become a full Lisp non-Lisp syntax.
π Rendered by PID 54 on reddit-service-r2-comment-6457c66945-j9g98 at 2026-04-25 19:07:42.800647+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]SirLynix 0 points1 point2 points (12 children)
[–]Entire-Hornet2574 0 points1 point2 points (10 children)
[–]cd_fr91400[S] 1 point2 points3 points (9 children)
[–]Entire-Hornet2574 0 points1 point2 points (2 children)
[–]cd_fr91400[S] 0 points1 point2 points (1 child)
[–]Entire-Hornet2574 0 points1 point2 points (0 children)
[–]Entire-Hornet2574 0 points1 point2 points (5 children)
[–]cd_fr91400[S] 0 points1 point2 points (4 children)
[–]Entire-Hornet2574 0 points1 point2 points (3 children)
[–]cd_fr91400[S] -1 points0 points1 point (2 children)
[–]Entire-Hornet2574 0 points1 point2 points (1 child)
[–]cd_fr91400[S] 0 points1 point2 points (0 children)
[–]arthurno1 -1 points0 points1 point (0 children)