简体   繁体   English

布尔表达式将要评估什么?

[英]What it is going to be evaluated in a boolean expression?

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 当我将biptr或我拥有的任何其他变量放在类似布尔值的表达式中时

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 ? C ++ 11标准说明了var1var2哪些部分决定了布尔结果? 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 我对如何将通用T类型转换为bool以及如何根据标准解决布尔表达式感兴趣

It's not clear to me what you're asking about. 我不清楚您要问什么。 A variable is true in C and C++ if it is non-zero. 如果变量非零,则在C和C ++中为true。 The && operator only evaluates the right operand if the left operand is true. &&运算符仅在左操作数为true时才评估右操作数。 The truth value of an instance of a class T doesn't exist unless there is a conversion function. 除非存在转换函数,否则不存在T类实例的真值。 This is all in the standard. 这一切都在标准中。

§5.14/1 §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 . 操作数都在上下文中转换为bool类型(第4章)...与&不同, &&保证从左到右求值:如果第一个操作数为false则不对第二个操作数求值。

§4/2 §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). 操作员对其操作数的要求决定了目标类型(第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 . 首先将var1隐式转换为bool ,然后,如果为true ,则将var2隐式转换为bool

Edit: I'll quote also §4.12/1 编辑:我也会引用§4.12/ 1

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool . 算术,无作用域枚举,指针或指向成员类型的指针的prvalue可以转换为bool类型的prvalue。 A zero value, null pointer value, or null member pointer value is converted to false ; 零值,空指针值或空成员指针值将转换为false any other value is converted to true . 其他任何值都将转换为true A prvalue of type std::nullptr_t can be converted to a prvalue of type bool ; 可以将std::nullptr_t类型的prvalue转换为bool类型的prvalue; the resulting value is false . 结果值是false

Edit 2: For some arbitrary type T , see §4/3, 编辑2:对于某些任意类型T ,请参见§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); 在这样的上下文中出现的表达式e被称为在上下文中转换为bool ,并且当且仅当声明bool t(e); ,它的格式才正确bool t(e); is well-formed, for some invented temporary variable t (8.5). 对于某些发明的临时变量t (8.5),其格式正确。

For the meaning of this initialization, see §8.5/16 有关此初始化的含义,请参见第8.5 / 16节

— Otherwise, if the source type is a (possibly cv-qualified) class type, conversion functions are considered. —否则,如果源类型是(可能是cv限定的)类类型,则考虑转换函数。 The applicable conversion functions are enumerated (13.3.1.5), and the best one is chosen through overload resolution (13.3). 列举了适用的转换函数(13.3.1.5),并通过过载分辨率(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. §13.3.1.5和§13.3甚至有更详细的内容,但您将通宵阅读一些内容。 But the bottom line is that if T is a class type, then there has to be a conversion function. 但最重要的是,如果T是类类型,则必须有一个转换函数。 The best practice would be to define explicit operator bool for the class. 最佳实践是为该类定义explicit operator bool 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. 但是,您还可以执行类似于operator void* ,这是std::ios及其派生类所定义的,因为然后可以按照标准转换序列将void*转换为bool (This should be regarded as a deprecated idiom in C++11.) (在C ++ 11中,这应视为不赞成使用的习惯用法。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM