2
f(-1);

int f(int a) {
    return (a%2) ? (a%2) : (a*2);
}

Consider the above function f() , a%2 is returned if a%2 is evaluated to non-zero.

QUESTION : Are there any advance operator or expression that can merge the condition and return value and form something like

return (a%2) OR (a*2)  // underlying logic is return (a%2) ? (a%2) : (a*2);

I understand that I have provided one possible solution return (a%2) ? (a%2) : (a*2); already but I want to explorer more on C++ so I ask this question. This question is simply a Yes/No question. I am expecting answers other then using if-else statement

My thought :

a%2 in a condition is evaluated and converted to type bool so it is no way to cast back to corresponding int value.

Biboo Chung
  • 121
  • 1
  • 2
  • 13

4 Answers4

3

You can use the fact that a Boolean expression results in 0 or 1 and use

int f(int a) {
    return (a > -1) * (a + 1);
}

If (a > -1) is true then we have 1 * (a + 1) otherwise we have 0 * (a + 1).

I would not suggest doing this though. Clear code should be one of the big things you try to maintain in you code. Using a well know syntax like condition ? true_result : false_result is better IMHO.

EDIT:

If you want the value to be something other than zero then it gets a little more complicated. You would need to add to the return value the value of the false condition times a Boolean expression that is only true when the true condition is false(a not condition).

int f(int a) {
    return ((a % 2) * (a % 2)) + (!(a % 2) * (a * 2));
    //     ^    true part    ^   ^    false part    ^
}

With this when the true part is 0 the false part will be a value and when the false part is 0 the true part will be a value.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I'm sorry, I've modified the question in order to make it clear so you may have to modify your answer also. – Biboo Chung Feb 16 '16 at 19:52
  • @BibooChung It is still the same type of thing. Multiply whatever you want in the true case against a condition that is only true when you want that value. – NathanOliver Feb 16 '16 at 19:57
  • @NathanOliver yes, you are right but what if I don't want to return 0 if the condition is false. – Biboo Chung Feb 16 '16 at 20:04
  • @BibooChung I updated the code. Again I would not use this but I figured it should at least be shown. – NathanOliver Feb 16 '16 at 20:33
2

Just write a function:

template<typename T>
T conditional( T &&v1, T &&v2 )
{
   return v1 ? v1 : v2;
}

int f( int a )
{
    return conditional( a%2, a*2 );
}
Slava
  • 43,454
  • 1
  • 47
  • 90
2

If you're on gcc, there's an extension for that:

int f(int a) {
    return (a%2) ? : (a*2);
}

is equivalent to:

int f(int a) {
    return (a%2) ? (a%2) : (a*2);
}

Clang seems to support it too. But really the compiler will probably do the right thing for you if you duplicate the expression. At the very least, this:

int f(int a) {
    auto&& expr = a%2;
    return expr ? expr : (a*2);
}

is more readable.

Barry
  • 286,269
  • 29
  • 621
  • 977
0

Actually that function always returns a+1. If a+1 is 0, you say you return 0, which is also a+1.

To answer what you mean though, you can always just do a bit of math if you prefer: !!cond * res1 + !cond * res2 for the general case of cond ? res1 : res2.

Blindy
  • 65,249
  • 10
  • 91
  • 131