Boolean "in-line"

I don't know how I can call a boolean which has been concatenated in one single line. Usually I talk about "in-line" boolean, but it's not the right expression because it's not literally an inline function. So... It's stupid, but I like it and when it is possible, I prefer this one to its alternative if/else. However sometimes it does not compile and I have an error which I cannot understand. Just a single example :

1
2
3
4
5
6
7
8
// check element in our vector
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  bool r;
  if ((find(vec.begin(), vec.end(), elem) != vec.end()) ? r = true : r = false);
  return r;
}

It works as expected - and it's logical... but the next does not work. Why?
1
2
3
4
5
6
// check element in our vector
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  if ((find(vec.begin(), vec.end(), elem) != vec.end()) ? return true : return false);
}

Maybe I wait too many, but where is the problem? I check a condition and I have two alternatives. I would like a final return. Is there an explication? Thanks for your help ++
The if has no purpose here. Writing the condition expression without the if will have the same result.

1
2
3
4
5
6
7
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  bool r;
  (find(vec.begin(), vec.end(), elem) != vec.end()) ? r = true : r = false;
  return r;
}

You don't even need to use a variable. The != operator already returns a bool so you could just return it directly.

1
2
3
4
5
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  return find(vec.begin(), vec.end(), elem) != vec.end();
}
Last edited on
Yes Peter87. I have in mind this one :
1
2
3
4
5
6
7
8
9
// check element in our vector
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
    if (find(vec.begin(), vec.end(), elem) != vec.end())
        return true;
    else
        return false;
}

Sorry for the mistake. However, in your example, we can omit parentheses too.
1
2
3
bool r;
find(vec.begin(), vec.end(), elem) != vec.end() ? r = true : r = false;
return r;

Your last example seems good. Thank you. But why I cannot use my previous code which used two alternative return ? This is the core of my question.
1
2
  bool r;
  (find(vec.begin(), vec.end(), elem) != vec.end()) ? r = true : r = false ;

The ternary operator does return a value. The above does not use it. It could:
1
2
  bool r;
  r = (find(vec.begin(), vec.end(), elem) != vec.end()) ? true : false ;

Of course, as said, the example is not useful since the condition already does return a boolean and you could write:
1
2
  bool r;
  r = find(vec.begin(), vec.end(), elem) != vec.end() ;

If one insist on using the if, then:
1
2
3
4
5
6
7
8
9
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  if ( find(vec.begin(), vec.end(), elem) != vec.end() ) {
    return true;
  } else {
    return false;
  }
}


Isn't the word "expression"?
1
2
if ( cond )
  statement

The condition cond is an expression.

Did the compiler say that return cannot be in expression or that it must be in statement?

You can write a statement:
return find(vec.begin(), vec.end(), elem != vec.end() ? true : false ;
where the find(vec.begin(), vec.end(), elem != vec.end() ? true : false
is an expression that is evaluated to get a value for return to return.
Last edited on
The conditional operator is a ternary operator, i.e. it takes three operands (arguments).

operand1 ? operand2 : operand3

If the first operand evaluates to true then it will evaluate the second operand and use that as the result.

true ? a : ba

If the first operand evaluates to false then it will evaluate the third operand and use that as the result.

false ? a : bb

The type of the second and third operand needs to be similar so that the result of the whole expression can be converted to a "common type" (e.g. int and double will result in double).

The reason your second code does not work is because all operands are expected to be expressions.

return true and return false are not expressions. If you had put semicolons at the end they would have been statements but a statement does not have a "type" and is not allowed here.

If all you really wanted was to write an if statement on a single line you can do that using a regular if statement. The C++ language does not prevent you from doing that although you might not see it that often due to readability/maintainability concerns.

1
2
3
4
5
template <typename T>
bool contains(vector<T> vec, const T& elem)
{
  if (find(vec.begin(), vec.end(), elem) != vec.end()) return true; else return false;
}
Last edited on
Thank you Peter87 and Keskiverto for your clever explanations.

The type of the second and third operand needs to be similar so that the result of the whole expression can be converted to a "common type" (e.g. int and double will result in double).
return true and return false are not expressions.

Now I understand clearly. The last example is clean. I like it too :)
Topic archived. No new replies allowed.