"Value" of Assignment

This code compiles without warnings:
1
2
int i;
bool b = (!(i = 42));


The variable b is assigned true if i is assigned 0, and false otherwise, because of the NOT operator.

However, I want the assignment to have the the opposite behavior without the NOT operator. If I try:

1
2
int i;
bool b = (i = 42);


It does what I want, but I get the warning:

warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)

I know I could use the NOT operator twice, or use the conditional operator but they seem redundant. Is there a more elegant way to do what I want?
shouldn't it be

1
2
bool b = (!(i ==42));
 
Disable the warning, write (i=42)!=0 or use static_cast.
I can't illustrate this error with my compiler so I think it is a compiler issue.My compiler is g++

gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
It's not an error, it's a warning. Do what Athar said. Disable it (not recommended), use the != operator, or use static_cast.
Topic archived. No new replies allowed.