is (false < true) ?

I'm writing a class to check for differences in discretes (bool), but I want it to be applicable for other types as well.

This prints in my compiler (VS2008), but is it gaurenteed in the C++99/03 std?

if (false < true) std::cout << "False is less than true!" << std::endl;


I'm trying to convert this:
1
2
3
4
5
6
7
8
9
10
bool Pulse::PosEdge(bool in)
{
    if (in &&  !m_oldVal)
    {
        m_oldVal = in;
        return true;
    }
    m_oldVal = in;
    return false;
}


into this:
1
2
3
4
5
6
7
8
9
10
bool Pulse<T>::PosEdge(T in)
{
    if(m_oldVal < in)
    {
        m_oldVal = in;
        return true;
    }
    m_oldVal = in;
    return false;
}


I've seen a compiler on Ubuntu which sends 0xff as true. If that was casted to a signed char, that would be negative and this wouldn't work. I want to ensure that doesn't happen.
Last edited on
false == 0
true == any number except 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
	int i = 0;

	if(2)
	{
			i=2;
			std::cout << i << std::endl;
	}
	if(0)
	{
		i=0;
		std::cout << i << std::endl;
	}
	if(-1)
	{
		i=-1;
		std::cout << i << std::endl;
	}
}

will give you the following output:
2
-1


edit: this also works with double, it's not limited to integer
Last edited on
I'm not 100% sure of this, but I believe what you want to do, OP, will work just fine.

Any nonzero value (when cast to a bool) gets cast to 'true'. true is a literal value of 1.

Example:

1
2
3
4
5
    bool temp = static_cast<bool>(-35);
    if(temp == 1)
    {
        cout << "yes";  // this prints
    }



Although since you are looking for a confirmation that this is guaranteed by the standard.... that I couldn't tell you. Sorry.
Values of type bool are either true or false. Values of type bool participate in integral promotions.

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

Note: These conversions are called integral promotions.

Note: Using a bool value in ways described by this International Standard as "undefined," such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.

Thanks guys! That's exactley what I wanted to know.

I had thought that the definition of true was simply non-zero and undefined beyond that.

I had tried this:
if (0xff == true) cout << "prints";
and it didn't print which now makes sense.
Topic archived. No new replies allowed.