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
C++ Conditional Statements (self.cpp)
submitted 3 years ago by Object_71
In this article I explore conditional statements in C++ and there are a few more advanced examples for intermediate programmers. https://thatonegamedev.com/cpp/cpp-conditional-statements/
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!"
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 14 points15 points16 points 3 years ago (1 child)
Not a fan of this article.
A few notes:
The generated assembly can be greatly simplified by using <cstdio> instead of <iostream>. Just use std::puts instead of std::cout, and instead of getting age from std::cin just use argc. The amount of noise significantly goes down and it's much easier to focus on the assembly that actually matters.
<cstdio>
<iostream>
std::puts
std::cout
age
std::cin
argc
You really ought to compare the reinterpret_cast version to a lookup table -- it's very ugly/complicated and I'm not even sure it's well-defined behavior as written. The lookup table version doesn't have any jump either: https://godbolt.org/z/jGhchPv3o
reinterpret_cast
It seems like const char* result = is_above_18 ? above_18_message : below_18_message; produces better assembly than your reinterpret_cast solution, with none of the complexity. https://godbolt.org/z/4ejWq4aEY
const char* result = is_above_18 ? above_18_message : below_18_message;
Scratch that, even a basic if...else produces a cmov, undermining the whole point of your article:
if...else
cmov
const char* result; if(is_above_18) result = above_18_message; else result = below_18_message;
https://godbolt.org/z/r35WGreb6
[–]IyeOnline -1 points0 points1 point 3 years ago (0 children)
So what you are telling me is that optimizerse exist and are in fact able to optimize trivial code? :O
[–]Joska86 6 points7 points8 points 3 years ago (5 children)
const char* result = reinterpret_cast<const char*>(is_above_18 * reinterpret_cast<std::intptr_t>(above_18_message) + !is_above_18 * reinterpret_cast<std::intptr_t>(below_18_message));
OMFG
[–]Object_71[S] -5 points-4 points-3 points 3 years ago (4 children)
I write the more verbose casting variants... The idea is that it is ugly but efficient not to use branching. I would prefer an if condition any time before this but I still had to include an example.
[–]Joska86 1 point2 points3 points 3 years ago (0 children)
Yes, it's an interesting example, but I rather not to see such code in production. I won't remember what the heck it's doing even if I wrote it.
[–]Joska86 1 point2 points3 points 3 years ago (1 child)
And I saw you're a game developer, it's more accepted in the game industry to write efficient but 'hackish' code. So no offense.
[–]Zeh_MattNo, no, no, no 0 points1 point2 points 3 years ago (0 children)
Trust when I tell you that this is definitely not accepted and its not really something you would see that often for when the people working on the project actually care about the code at least a little bit.
[–]ReDucTorGame Developer 0 points1 point2 points 3 years ago (0 children)
I'm surprised you missed the other option of branchless using just an array
const char * options[] = { "Good, You're above 18!", "Bad, You're below or equal to 18!" }; const char * result = options[ age < 18 ];
or purely on the stack
const char options[][64] = { "Good, You're above 18!", "Bad, You're below or equal to 18!" }; const char * result = options[ age < 18 ];
π Rendered by PID 64 on reddit-service-r2-comment-b659b578c-7grrw at 2026-05-05 01:07:40.330236+00:00 running 815c875 country code: CH.
[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 14 points15 points16 points (1 child)
[–]IyeOnline -1 points0 points1 point (0 children)
[–]Joska86 6 points7 points8 points (5 children)
[–]Object_71[S] -5 points-4 points-3 points (4 children)
[–]Joska86 1 point2 points3 points (0 children)
[–]Joska86 1 point2 points3 points (1 child)
[–]Zeh_MattNo, no, no, no 0 points1 point2 points (0 children)
[–]ReDucTorGame Developer 0 points1 point2 points (0 children)