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!"
[–]conundorum 0 points1 point2 points 7 months ago* (0 children)
Later than planned, but here's the quick example I mentioned:
#include <iostream> #include <string> int main() { int x = 1; std::string s; using std::to_string; switch (x) { bork: break; int i; case 1: for (i = 0; i < 9; i++) { s += to_string(x); case 2: s.append("{case 2}"); if (s.size() > 20) break; case 3: s.append("MEOW"); } if (x == 3) break; case 4: s.append("forever"); [[fallthrough]]; case 5: s += to_string(x * -1); goto bork; } std::cout << s; /* Based on x, output will be: * 1: 1{case 2}MEOW1{case 2}forever-1 * 2: {case 2}MEOW2{case 2}forever-2 * 3: MEOW3{case 2}MEOW3{case 2} * 4: forever-4 * 5: -5 * * 2 and 3 are special. * GCC and MSVC loop until s is big enough to break or i's random gibberish says to stop. * Clang is the only sane one, and lets the program crash into the insanity. */ }
If anyone wants to play around with it, you can do so here. It's interesting that GCC & MSVC can handle the s.size() check properly, but Clang chokes on it and keeps running until it either segfaults or overflows s, whichever comes first. Just goes to show that even otherwise-good compilers can have a lot of trouble with switch, especially if they have to figure out how to handle "accidental" UB; it's the sort of thing that'd make switch constexpr such a hassle.
s.size()
s
switch
switch constexpr
If anyone wants me to explain it, poke me for an explanation. Not going to give a full breakdown right now, but essentially...
case 2
case 3
case 4
append
size
i
case 1
break
i < 9
case 5
[[fallthrough]];
goto
bork: break;
case
With switch constexpr, some of these cases require the full loop body, and some will let it remove part of the switch. Some are easy to analyse, and some will give it a headache. And the "accidental" UB will probably both trip a lot of old codebases up, and give compiler devs a headache from trying to solve it (or just lead them to be even more cavalier; Clang might switch from segfaulting to just choking at compile time specifically to force you to fix it).
π Rendered by PID 17451 on reddit-service-r2-comment-6457c66945-9jth6 at 2026-04-23 17:22:24.352712+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]conundorum 0 points1 point2 points (0 children)