Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more C++ elegant implementation to complete the function jump?

In my daily work, I usually write my code like this:

int ret = 0;
ret = func1();
if(ret != 0) {
  return ret;
}
ret = func2();
if(ret != 0) {
  return ret;
}

But this means I need to complete a lot of "if(ret!=0){return ret;}",Is there a more C++ elegant implementation to complete the function jump? By the way, we are not allowed to use exception.

like image 428
komonzhang Avatar asked Dec 11 '22 00:12

komonzhang


1 Answers

The shown code seems to be logically equivalent to:

int ret;

if ((ret=func1()) || (ret=func2()))
    return ret;

Tack on as many additional function calls as you wish, in the obvious manner.

like image 183
Sam Varshavchik Avatar answered May 11 '23 01:05

Sam Varshavchik