jana
jana

Reputation: 81

Assert is not working

My assert is simply not working.

int tspace::Tpiz::set_pitPoz(int p)
{
    assert (0<=p<=11);
    pitPoz = p;
}

In main:

Tpiz piz;
piz.set_pitPoz(78);
cout << piz.get_pitPoz();

The output is:

78
- - - - - - - - - - - - - -
Process exited after 0.03378 seconds with return value 0
Press any key to continue

Is there something else I need to do?

Upvotes: 1

Views: 482

Answers (1)

Mike Nakis
Mike Nakis

Reputation: 62129

The expression 0<=p<=11 probably evaluates 0<=p, which yields a bool, but then it tries to involve this bool in a comparison against 11, which is an integer, so it promotes the bool to int, (0 or 1,) and then checks to see whether this 0 or 1 is less than or equal to 11. So, it will always succeed.

You might be able to avoid stupid accidents of that sort by enabling more warnings, so that the compiler will warn you that you are most likely doing something wrong. Try -Wall, or whatever it is that tells your compiler to enable all warnings. You cannot be trying to write software without many, preferably most, warnings enabled.

Upvotes: 3

Related Questions