在 C++ 中是否像在 Java 中一样保证短路评估? [英] Is Short Circuit Evaluation guaranteed In C++ as it is in Java?

查看:33
本文介绍了在 C++ 中是否像在 Java 中一样保证短路评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,我使用

In Java, I use

if (a != null && a.fun());

充分利用短路求值和表达式从左到右求值?

by taking full advantage of short-circuit evaluation and expression are evaluated from left to right?

在 C++ 中,我可以这样做吗?它们是否保证可以跨不同平台和编译器移植?

In C++, can I do the same? Are they guarantee to portable across different platform and compiler?

if (a != 0 && a->fun());

推荐答案

是的,对于内置"类型是有保证的.但是,如果您重载 &&或 ||对于您自己的类型,不执行短路评估.因此,重载这些运算符被认为是一件坏事.

Yes, it is guaranteed for the "built in" types. However, if you overload && or || for your own types, short-circuited evaluation is NOT performed. For this reason, overloading these operators is considered to be a bad thing.

这篇关于在 C++ 中是否像在 Java 中一样保证短路评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆