"if (var)" uses number conversion instead of boolean

Why if (var) ... uses number conversion instead of boolean? I have a class that implements both:

operator int() { ....} operator bool() { ....} 

but if i use:

 if (my_class_var) ....; 

instead, an int conversion is used instead of boolean ? !!?!

EDIT: As versedmarald says this is correct. I found that was different. I actually use:

 operator int() { ....} operator bool() const { ... } 

Still mesmerized, why the difference? gcc version 4.6.2

+4
source share
3 answers

This is not the case (using g++ at least). I assume there is an error in your conversion operations.

 #include <iostream> class A { public: operator int() { return 1; } }; class B { public: operator int() { return 1; } operator bool() { return false; } }; int main() { A a; B b; if (a) std::cout << "true\n"; else std::cout << "false\n"; if (b) std::cout << "true\n"; else std::cout << "false\n"; } 

Outputs:

 true false 
+1
source

If you are telling the truth, I believe your compiler is breaking the standard:

(ยง6.4 / 4) The value of the condition, which is the initialized declaration in a statement other than the switch statement, is the value of the declared variable, contextually converted to bool (section 4). If this transformation is poorly formed, the program is poorly formed. [...]

(To be clear, this is in the context of ยง6.4, which describes the if and switch .)

+3
source

There are two used-defined implicit conversion chains .

First - class -> bool -> no conversion

Second - class -> int -> bool

n3337 4/2

Note: expressions with the given type will be implicitly converted to other types in several contexts:

- When using the if statement or iteration in a condition (6.4, 6.5). The destination type is bool.

n3337 4/3

The effect of either an implicit conversion is the same as performing a declaration and initialization, and then using a temporary variable as a result of the conversion.

Quotes mean that really

 if (class_var) 

is an

 if (bool _ = class_var) 

n3337 13.3.3 / 1

Given these definitions, a viable function F1 is defined as a better function than another viable function F2, if for all arguments i, ICSi (F1) is not a worse transformation sequence than ICSi (F2), and then

- the context is initialization by user conversion (see 8.5, 13.3.1.5 and 13.3.1.6) and the standard conversion sequence from the return type F1 to the destination type (i.e., the initialization type of the object) is a better conversion sequence than the standard conversion sequence from return type F2 to destination type. [Example:

 struct A { A(); operator int(); operator double(); } a; int i = a; // a.operator int() followed by no conversion //is better than a.operator double() followed by //a conversion to int float x = a; //ambiguous: both possibilities require conversions, //and neither is better than the other 

- end of example

So, the compiler should choose operator bool , since class -> bool -> no standart conversion better than class -> int -> standard conversion to bool

+1
source

Source: https://habr.com/ru/post/1434135/


All Articles