简体   繁体   English

该布尔表达式如何求值?

[英]How is this boolean expression evaluated?

In C++, let the following construct : 在C ++中,让以下构造:

template<typename ValueType>
ValueType * func(Foo * foo)
{
    Bar bar;
    return foo && typeid(foo) == typeid(ValueType) ? &static_cast<ValueType*>bar : 0;
}

How is the return statement evaluated? 如何评估return语句? Like so? 这样吗

if ((bar && typeid(bar)) == typeid(ValueType))
    return &static_cast<ValueType*>bar
return false;
foo && typeid(foo) == typeid(ValueType) ? &static_cast<ValueType*>bar : 0;

...corrected with parenthesis after the static_cast<> , is evaluated as... ...在static_cast<>之后用括号纠正,其评估为...

(foo && (typeid(foo) == typeid(ValueType))) ? (&(static_cast<ValueType*>(bar))) : 0;

The precedence rules are listed here . 优先级规则在此处列出。 Notice that the ?: ternary operator is at precedence level 15 in this list - lower than the other operators you've used, so it defines the outer structure of the evaluation. 请注意, ?:三元运算符在此列表中的优先级为15-低于您使用的其他运算符,因此它定义了求值的外部结构。 && is at 13 - way below == at 9. (I don't think these numbers are used anywhere in the Standard, but they're convenient references for pointing out things in the cppreference table). &&在13处-低于==在9处。(我认为这些数字在标准中的任何地方都没有使用,但是它们是指出cppreference表中内容的便捷参考)。

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

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