Initializations in if
One of the capabilities added in C++17 was using initializations in if/else statements: if (auto foo = f(); foo.isValid()) ... The primary use of this is to restrict the scope of the variables (which can include scoping variables like lock guards). However, it also provides for some slightly neater and better self-documenting code. There's a fundamental difference between Foo f = func(); if (!f.isNull()) { doSomething(f); } else { f = func2(); doSomethingElse(f); } //Use f more generally And the same block where f is not used again: the example above is a creation pattern with side effects. It could be rewritten as std::pair<Foo, bool> MakeFoo() { if (Foo f = func(); !f.isNull()) { return std::make_pair(f, true); } else { return std::make_pair(func2(), false); } } ... auto [f, flag] = MakeFoo(); if (flag) doSomething(f); else doSomethingElse(f); ... where the creation logi...