Question

assuming that I have the following variables

bool b;
int i;
T* ptr;

when I put b, i or ptr, or whatever other variables I have, in a boolean expression like

var1 && var2

What is going to be evaluated to solve this expression ? The C++11 standard says something about what is the part from var1 and var2 that contributes to determine the boolean result ? There are implicit casting ?

It's not clear to me what the standard says about this.


EDIT

I'm interested in how a generic T type is converted into a bool and how a boolean expression is solved according to the standard

Was it helpful?

Solution 2

§5.14/1

The && operator groups left-to-right. The operands are both contextually converted to type bool (Clause 4)... Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

§4/2

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

— When used as operands of operators. The operator’s requirements for its operands dictate the destination type (Clause 5).

...

This is about as clear as the standard gets. First var1 will be implicitly converted to bool, and then, if it is true, var2 will be implicitly converted to bool.

Edit: I'll quote also §4.12/1

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

Edit 2: For some arbitrary type T, see §4/3,

... Certain language constructs require that an expression be converted to a Boolean value. An expression e appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the declaration bool t(e); is well-formed, for some invented temporary variable t (8.5).

For the meaning of this initialization, see §8.5/16

— Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered. The applicable conversion functions are enumerated (13.3.1.5), and the best one is chosen through overload resolution (13.3). The user-defined conversion so selected is called to convert the initializer expression into the object being initialized. If the conversion cannot be done or is ambiguous, the initialization is ill-formed.

There is even more detail in §13.3.1.5 and §13.3, but you will be reading all night and then some. But the bottom line is that if T is a class type, then there has to be a conversion function. The best practice would be to define explicit operator bool for the class. However you could also do something like operator void*, which is what std::ios and its derived classes define, because void* can then be converted to bool in a standard conversion sequence. (This should be regarded as a deprecated idiom in C++11.)

OTHER TIPS

It's not clear to me what you're asking about. A variable is true in C and C++ if it is non-zero. The && operator only evaluates the right operand if the left operand is true. The truth value of an instance of a class T doesn't exist unless there is a conversion function. This is all in the standard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top