Reputation: 359
Please consider the following snippet
//foo is just a random function which returns error code
if(int err = foo() == 0){
//err should contain the error code
...
}
The problem with that is err
contains the result of foo() == 0
, and what I want to evaluate is int err = foo();
then if(err == 0)
but inside the if
statement.
I have already tried if((int err = foo()) == 0)
and it doesn't work.
Also, I don't want to do int err; if((err = foo) != 0))
.
Is this possible?
Upvotes: 1
Views: 710
Reputation: 306
For C++ 17 and onwards you can use if statements with initializers:
if (int err = foo(); err == 0) {
...
}
Upvotes: 3