0

What do the expressions in C++ return, the actual value obtained after applying the operators on the objects or true/false based on the calculated value ? Particularly :

In this code segment :

int i = 10, j = 5, k = 0;
i + j ;
k = i + j ;
while ( i + j )
{
    // Do something worth for the universe in this time
}

What will the expression written in line No. 2 return, 15 or true ? Will it return the same value in line No. 4 ? Is 15 always returned, but converted to true or false based on the context ?

I read this in C++ Primer :

A while has the form

while (condition)
     statement   

A while executes by (alternately) testing the condition and executing the associated statement until the condition is false. A condition is an expression that yields a result that is either true or false.

But expressions could be just plain objects as well right !? How are they supposed to mean a true or false ? For eg:

// Create an object `tempObject` of a class `SomeRandomClass`
while ( tempObject )
{

}

Can someone explain ?

Amit Tomar
  • 4,800
  • 6
  • 50
  • 83

4 Answers4

4

When the "condition" in an if or while is not a direct boolean value (true or false) from the expression itself (== or > or <=, for example, would give a boolean value), the expression is seen as if it was a comparison as not equal to zero.

So:

int x = 25;

while(x)
 ... 

is the same as

while (x != 0)

This applies to all types, such as pointers, integers and floating point values.

Edit as per comments below:

An object of a class or struct must be possible to convert into a bool in some way: Either the object has direct operator bool() like this:

class X
{
  private:
    int x;
  public:
    X(int xx = 0) x(xx) {}
    void setx(int xx) { x = xx; }
    bool operator bool() const { return x != 0; }
};

Now we can do:

X a(1);

if (a) ... 
a.setx(0);
if (a) ...

The first if will be true, the second if will be false.

If there isn't an operator bool() available, then the compiler may use operator int() or operator void* to convert the type, and then making the implicit != 0 comparison to produce a bool value.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Is an object treated in the same way ? eg. `while (object){}` ? Will that necessarily require `!=` (or `==`) operator to be defined for that object ? – Amit Tomar Jun 12 '13 at 15:19
  • If an object can't be turned into something that can be turned into a `bool`, then it will give an error. It requires an `operator int()` or `operator void*()` or similar to convert it to something that can be compared to 0. – Mats Petersson Jun 12 '13 at 15:21
  • 1
    Not entirely true that it's necessarily compared to 0. The expression in if/while statements will be tried to be converted to a boolean value. For integers, there is an implicit conversion, meaning 0 => false, everything else => true; but for objects, it's a different matter. There could e.g. be an explicit `operator bool()` as well – codeling Jun 12 '13 at 15:21
  • Yes, that's what I meant, if there ISN'T an `operator bool()`, the compiler will pick something that can be converted to `bool` by using (an implicit) ` != 0`. If there is "nothing", then there will be an error "can't convert object to bool" or something like that. – Mats Petersson Jun 12 '13 at 15:30
  • I agree with @nyarlathotep. The whole "!=0" explanation is redundant. If the expression has type `bool`, it's used directly. If the expression can be converted to `bool`, then it's converted. Else, it's a type error. – MSalters Jun 12 '13 at 23:01
2

0 (and false, in versions of C++ where that exists) means false; any other value means true.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Does `false` not exist in all versions of C++? I thought it was only C that introduced boolean literals later. – chris Jun 12 '13 at 15:08
  • I know what you have said as a fact, but wanted to understand it. I wanted to understand how I should be approaching expressions and treat the output they produce. eg if condition would have been `while (std::cin >> value)`, what you are saying will hold true ? – Amit Tomar Jun 12 '13 at 15:08
  • @chris: I'm pretty sure that is was not there at the beginning. – SLaks Jun 12 '13 at 15:09
  • @AmitTomar, That returns a `std::istream &` (`std::cin` in this case), which has `explicit operator bool()` (`operator void *()` in C++03) that converts its state to a boolean value. – chris Jun 12 '13 at 15:09
  • @chris Can you please explain what you mean by `which has explicit operator bool() that converts its state to a boolean value` ? – Amit Tomar Jun 12 '13 at 15:21
  • @AmitTomar, You might want to see [this](http://stackoverflow.com/questions/8239356/can-a-cast-operator-be-explicit). It allows your class to be explicitly converted to a `bool`. – chris Jun 12 '13 at 15:25
1

To understand these code, please first go back to C:

Everthing which is 0 is false. Everthing which is not 0 is true.

In your case the while loop then means while ( 15 ) which is the same as while ( true )

An endless loop...

Giving an object into such a code the object must behave at least as an integer or an boolean giving the while loop any evaluable expression, e.g.

public:
    operator bool() const
    {
         return myVar;
    }

where you store the while condition in myVar ...

1

i + j has type int because both i and j are ints. The value of the expression is the sum of the two. In the statement

i + j;

the value is not used, so it is ignored. Chances are the compiler will omit the addition, since it has no side effects. In the statement

k = i + j;

the result is used; it is copied into k. In the statement

while (i + j) {
}

the value is used in a boolean context. Formally, it is "contextually converted to bool": zero is converted to false and any other value is converted to true.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165