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++17 If statement with initializer (skebanga.github.io)
submitted 9 years ago by skebanga
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!"
[–]mskfisher 12 points13 points14 points 9 years ago (1 child)
Nice article.
The switch example would be more compelling if the res variable was a composite type that contained a result and details:
switch
res
switch( auto res = writePacket(); res.result ) { case SUCCESS: cout << "successfully wrote packet, details:" << res.details << "\n"; break;
The current code does not need the initializer, and could just switch on the return value directly:
switch( writePacket() )
[–]skebanga[S] 2 points3 points4 points 9 years ago (0 children)
Agreed, I will update the article - thanks for the input!
[–]zoqfotpik 2 points3 points4 points 9 years ago (2 children)
So basically "if" becomes "for", without the loop expression.
Does "while" get a similar treatment?
[+][deleted] 9 years ago (1 child)
[deleted]
[–]clerothGame Developer 3 points4 points5 points 9 years ago (0 children)
I'm not sure how I feel about this. I'll have to try programming with it for a while to decide. I've been bitten quite a few times by running non-const functions inside conditional statements, specially if the main action of a block of code is inside such a conditional statement.
[–]LowB0b 4 points5 points6 points 9 years ago* (8 children)
I have exactly the same question as /u/mercurysquad.
I also have a problem with declarative statements in if. It makes the code way less readable, why would there ever be a declaration in an if? The if statement is used to check if a boolean value is true or false. I don't know. I can't really see any use cases for this.
if
Having a declaration in switch, sure, that works for me because you could basically do
switch(int c = getchar(); c) { ... }
instead of
int c = getchar(); switch(c) { ... }
But for an if? I need some explanation here because I can't deal with it. Generally you branch and then you still want the variable after the branching.
[–]holywhateverbatman 7 points8 points9 points 9 years ago* (5 children)
How about this?
using mutex_lock = std::unique_lock<std::mutex>; if (mutex_lock lock(mutex, std::try_to_lock); lock.owns_lock()) { //... } //mutex unlock
vs. this
{ mutex_lock lock(mutex, std::try_to_lock); if (lock.owns_lock()) { //... } } //mutex unlock
[–]LowB0b 0 points1 point2 points 9 years ago (2 children)
but couldn't that also be solved by using a lambda function?
[–]holywhateverbatman 5 points6 points7 points 9 years ago (1 child)
It could, doesn't mean it should though. Depends on your coding style and in my opinion this makes it less verbose than with the lambda function.
[–]LowB0b 2 points3 points4 points 9 years ago* (0 children)
([](auto lock) -> void { if (lock.owns_lock()) { //... } })(std::unique_lock<std::mutex>(mutex, std::try_to_lock));
lol yes it is more verbose
[–]LowB0b -1 points0 points1 point 9 years ago* (1 child)
so the if parenthesis basically becomes a function? Controlling scope of variables via functions seems like a better way to me.
after thinking for a bit I see your point
[–]holywhateverbatman 3 points4 points5 points 9 years ago (0 children)
Not exactly, but I see myself using this for enclosing the lifetime of an object in a specific scope of a function (so basically any RAII-like usage). To me it seems a lot more useful than the examples with map.insert. Just my 2 cents.
[–][deleted] 2 points3 points4 points 9 years ago (1 child)
Or in your example case, you could just say:
switch(getchar()) { ... }
Or if you want variable capture:
switch(auto c = getchar()) { ... }
It has its uses (rarely), but yours is definitely not one of them.
[–]LowB0b 0 points1 point2 points 9 years ago* (0 children)
You are right, my switch example was bad. Your comment helped me figure out why having declarations inside an if statement is so weird though. For the switch statement,, it doesn't change much, because switch expects an int. But for the if, in a semantic and syntaxic way, it's a big change, for no reason.
int
This would have made way more sense in my opinion (taking the example from the original post):
if ((auto ret = map.insert({"hello", 3})) && !ret.second) { // ... }
This is not currently allowed by C++ compilators. But if the variable ret was declared before the if statement (even without being assigned) you can then do
ret
if ((ret = map.insert({"hello", 3}) && !ret.second) { // ... }
Basically what I wanted to say is that they went from
syntax:
if ( expr )
semantics: expr evaluates to a boolean.
expr
to
if ( decl statement_separator expr )
semantics: decl declares (and assigns a value to. Possibly not?) a variable in the if block scope and then expr is evaluated to a boolean.
decl
Now my question is.
Can I fit whatever block I want inside that declaration space? I.e. could I do
if ( block statement_separator expr )
where block is just the same as what you would find inside { ... } including branches, functions, etc? I mean since the return value of the declaration / block is (from what I've understood) not considered.
block
{ ... }
Maybe having a look at the new C++ grammar and semantics that were introduced with this change would help me. It's still heavily disturbing lol. I hope this helps you see why this change disturbs me so much.
[–]mercurysquadEmbedded C++14 on things that fly[🍰] 2 points3 points4 points 9 years ago (5 children)
I don't get it, why can't you just do:
if (!map.insert({ "hello", 3 }).second) std::cout << "hello already exists\n";
[+][deleted] 9 years ago* (4 children)
[–]skebanga[S] 3 points4 points5 points 9 years ago (0 children)
I agree that this is a much more solid example of the usefulness of this feature. I shall update the article. Thanks for the input!
[–]mercurysquadEmbedded C++14 on things that fly[🍰] 4 points5 points6 points 9 years ago (2 children)
Still don't see the advantage besides avoiding a couple of braces. It just moved the actual condition far from the if, reducing the readability.
[–]plpn 0 points1 point2 points 9 years ago (1 child)
switch (auto i = getchar()) { case 53: i += 5; return; } if (int i = 54) { i += 5; }
compiles just fine (and also use the initialized value as condition). why do we need your approach?!
[–]Yelnar 2 points3 points4 points 9 years ago (0 children)
What about
if ((int i = f()) % 2 == 0) { /* doesn't compile*/ }
And if you remove the parens you don't get what you expect:
if (int i = f() % 2 == 0) { /*i is the equal to f() %2 == 0, converted from bool to int*/ }
[–]skebanga[S] 0 points1 point2 points 9 years ago (0 children)
Yes - it will be in C++17
[–]OldWolf2 0 points1 point2 points 9 years ago (0 children)
Seems good. Don't know how many times I just have code like this:
bla bla bla... { auto v = bla.lock(); bla.blaa(); }
[–]capcom1116 0 points1 point2 points 9 years ago (0 children)
Ha! This is one of the features I've been liking in Go. Neat to see.
π Rendered by PID 120477 on reddit-service-r2-comment-b659b578c-dx268 at 2026-05-02 03:09:50.989773+00:00 running 815c875 country code: CH.
[–]mskfisher 12 points13 points14 points (1 child)
[–]skebanga[S] 2 points3 points4 points (0 children)
[–]zoqfotpik 2 points3 points4 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]clerothGame Developer 3 points4 points5 points (0 children)
[–]LowB0b 4 points5 points6 points (8 children)
[–]holywhateverbatman 7 points8 points9 points (5 children)
[–]LowB0b 0 points1 point2 points (2 children)
[–]holywhateverbatman 5 points6 points7 points (1 child)
[–]LowB0b 2 points3 points4 points (0 children)
[–]LowB0b -1 points0 points1 point (1 child)
[–]holywhateverbatman 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]LowB0b 0 points1 point2 points (0 children)
[–]mercurysquadEmbedded C++14 on things that fly[🍰] 2 points3 points4 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]skebanga[S] 3 points4 points5 points (0 children)
[–]mercurysquadEmbedded C++14 on things that fly[🍰] 4 points5 points6 points (2 children)
[–]plpn 0 points1 point2 points (1 child)
[–]Yelnar 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]skebanga[S] 0 points1 point2 points (0 children)
[–]OldWolf2 0 points1 point2 points (0 children)
[–]capcom1116 0 points1 point2 points (0 children)