在同一 if 语句中检查空指针 && 指针-> 成员值
Checking null pointer && pointer->member value in the same if statement
这是一个非常基本的问题,但我找不到明确的答案:
是否允许检查指针不为 null 和 (&&) 以在同一个 if 语句中检查其成员值之一?
换句话说:如果 f 为空,下面示例中条件的右侧部分是否会被评估?
我知道这在 VS 当前版本中有效,但我需要知道这是否为 C++ 标准所允许(或者它是否为 UB)。
另外,如果我将它写成 2 个单独的 if 以使其更具可读性,我是否可以期望编译器将其优化为单个 if?
struct foo
{
bool bar;
};
void main(){
foo *f;
// DO THINGS
if (f != null && f->bar == true)
{
// DO THINGS
}
}
编辑:这个问题与 this one 不同,因为它不明显只是一个顺序问题:证据是当我用谷歌搜索我的问题时,我并没有以那个 SO 答案结束。
...is it allowed to check that a pointer is not null and (&&) to also check one of its members value in the same if statement?
It's perfectly valid, it is not UB,表达式从左到右计算,如果表达式的左边部分计算为 false
,则不计算右边部分。这通常被称为运营商短路。
理由是,如果第一部分是false
,就不可能整个表达式是true
,false && false
是false
,false && true
也是 false
.
...if I write it as 2 separate if to make it more readable, can I expect the compiler to optimize it into a single if?
根据上述答案,您不需要两个 if
,我认为这不会使您的代码更具可读性,我更喜欢您正确知道的方式,在任何情况下如果这只是我的意见。关于编译器,我不认为这两种方式会有太大区别,如 live demo.
中所示
is it allowed to check that a pointer is not null and (&&) to also check one of its members value in the same if statement?
是的。
is the right part of the condition in the example below even evaluated if f is null?
没有
what I need to know is if this is allowed by the C++ standard
是的。
(or if it's UB)
没有
Also, if I write it as 2 separate if to make it more readable, can I expect the compiler to optimize it into a single if?
我希望如此。似乎是一个微不足道的优化。请注意,在 &&
运算符被重载的情况下(在指针的情况下永远不会),这种更改会改变程序的含义。
标准报价(最新稿):
[expr.log.and]
The && operator groups left-to-right.
The operands are both contextually converted to bool.
The result is true if both operands are true and false otherwise.
Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.
这是一个非常基本的问题,但我找不到明确的答案: 是否允许检查指针不为 null 和 (&&) 以在同一个 if 语句中检查其成员值之一?
换句话说:如果 f 为空,下面示例中条件的右侧部分是否会被评估?
我知道这在 VS 当前版本中有效,但我需要知道这是否为 C++ 标准所允许(或者它是否为 UB)。 另外,如果我将它写成 2 个单独的 if 以使其更具可读性,我是否可以期望编译器将其优化为单个 if?
struct foo
{
bool bar;
};
void main(){
foo *f;
// DO THINGS
if (f != null && f->bar == true)
{
// DO THINGS
}
}
编辑:这个问题与 this one 不同,因为它不明显只是一个顺序问题:证据是当我用谷歌搜索我的问题时,我并没有以那个 SO 答案结束。
...is it allowed to check that a pointer is not null and (&&) to also check one of its members value in the same if statement?
It's perfectly valid, it is not UB,表达式从左到右计算,如果表达式的左边部分计算为 false
,则不计算右边部分。这通常被称为运营商短路。
理由是,如果第一部分是false
,就不可能整个表达式是true
,false && false
是false
,false && true
也是 false
.
...if I write it as 2 separate if to make it more readable, can I expect the compiler to optimize it into a single if?
根据上述答案,您不需要两个 if
,我认为这不会使您的代码更具可读性,我更喜欢您正确知道的方式,在任何情况下如果这只是我的意见。关于编译器,我不认为这两种方式会有太大区别,如 live demo.
is it allowed to check that a pointer is not null and (&&) to also check one of its members value in the same if statement?
是的。
is the right part of the condition in the example below even evaluated if f is null?
没有
what I need to know is if this is allowed by the C++ standard
是的。
(or if it's UB)
没有
Also, if I write it as 2 separate if to make it more readable, can I expect the compiler to optimize it into a single if?
我希望如此。似乎是一个微不足道的优化。请注意,在 &&
运算符被重载的情况下(在指针的情况下永远不会),这种更改会改变程序的含义。
标准报价(最新稿):
[expr.log.and]
The && operator groups left-to-right. The operands are both contextually converted to bool. The result is true if both operands are true and false otherwise. Unlike &, && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.