over_optimistic
over_optimistic

Reputation: 1419

c++ return trick

In c/c++ if a function is boolean you can do a trick like this

function1() || function2();

so if function1() fails function2() is run. I remember there is also a trick to do something like

function() || return false;

But "return false" does not give a result back. This kind of stuff is common in other languages like perl.

Question

function() || (x);

what the I put in x so that I get the equivalent of

if(function()==false)
    return false;

Edit

I'm not going to select an answer and let the vote bubble people up. Anyway here's the solution:

first put this somewhere

#define treturn(x) ({return (x); 1;})

And now you can do this

function() || treturn(false)

or return what ever you please. Where the treturn part happens only if function()==false. And you can use "&&" aswell

function() && treturn(false)

treturn in this case will happen when function()==true.

Note As many people have already mentioned. You shouldn't do this. And ds27680 posted a good alternative

Upvotes: 1

Views: 1307

Answers (4)

ds27680
ds27680

Reputation: 1993

The simplest way to get the equivalent of:

if(function()==false)
    return false;

would be:

return function();

:-)

Now getting serious... and to address the comments, if you have code after the if then in my humble opinion you already wrote the form most C++ developers would have no readability problems with (for completeness sake):

if(function()==false)
    return false; 

or:

   if ( !function() )
      return false; 

Anything else is either not possible or of dubious value in my opinion...

Upvotes: 1

John Dibling
John Dibling

Reputation: 101506

This might not be the answer you're really looking for, but I think it's better to do this:

return function();

...or, if you're feeling verbose:

if( function() )
  return true;
else 
  return false;

Playing little tricks with the language syntax will only get you in trouble, or punched by a future maintenance programmer down the line.

EDIT: Per your comments here, if you want to return false if the function evaluates to false or else continue execution if it evaluates to true, then there can be no simpler, more performant or easer-to-maintain way to do this than:

if( !function() )
  return false;

/*
 * MAGIC HAPPENS!
 */

EDIT2: If you are trying to avoid multiple return points, then simply:

if( function() )
{ 
  /*
   * MAGIC HAPPENS!
   */
}

Upvotes: 3

James Kanze
James Kanze

Reputation: 154047

I'm trying to figure out where the "trick" is. In C++, operators || and && are defined to be short-circuiting, and it is standard practice to use them as such, e.g. for things like:

return p == NULL || *p == '\0';

Of course:

return function() || return false;

is illegal—it is possible in Perl, where it is one of the things that make Perl a write only language.

return function() || false;

is legal, syntactically, but meaningless; it is the exact equivalent of:

return function();

And your last example should be written:

if ( !function() ) {
    return false;
}

. Or more likely with something along the lines of:

bool retval = true;
if ( function() ) {
    //  ...
    retval = ...
}
return retval;

Why obfuscate when you can make it clear?

Upvotes: 4

Rob Audenaerde
Rob Audenaerde

Reputation: 20109

In case of just false return function()

Else

return function() || function2()

But beware, I'm not sure if the order of the OR statement is defined.

If it is, and function() returns true, function2() is not evaluated.

Upvotes: 1

Related Questions