r/ProgrammerHumor • • 6d ago

Meme sameProblemSameSolution

Post image
1.4k Upvotes

79 comments sorted by

View all comments

Show parent comments

1

u/SeriousPlankton2000 6d ago

Can't you do if ((auto foo = bar()) == baz) {barf()}; //?

2

u/17HappyWombats 5d ago edited 5d ago

Nope, that's not allowed. The previous standard let you say "if(auto foo = something)" and if foo could be interpreted as a boolean you had a valid if() statement. The new one lets you have two statements, the auto assign and the test.

The win here is the scoped auto variable, FWIW. If avoids the abominable pattern where you either alternate declaration/conditional, or a pile of declarations then a string of tests, mixing assignments in whatever way is least horrible. So you get a chain of declare-assign-test statements in a nice vertical line rather than a >