I was just shocked, that this is allowed:
if( int* x = new int( 20 ) )
{
std::cout << *x << \"!\\n\";
// delete x;
}
else
{
std::cout
Not really an answer (but comments are not well suited to code samples), more a reason why it's incredibly handy:
if (int* x = f()) {
std::cout << *x << "\n";
}
Whenever an API returns an "option" type (which also happens to have a boolean conversion available), this type of construct can be leveraged so that the variable is only accessible within a context where it is sensible to use its value. It's a really powerful idiom.