简体   繁体   English

C++ 中的返回表达式检查条件

[英]Return Expression Check Condition in C++

Still getting used to the formatting when writing C++ code, come from a Lua background.在编写 C++ 代码时仍然习惯于格式化,来自 Lua 背景。 How do I correctly format a if/conditional expressions as my example highlights below.我如何正确格式化 if/条件表达式,如下面的示例突出显示的那样。

This will correctly run, but with warnings which is unideal:这将正确运行,但会出现不理想的警告:

return (boolean == true) and init() or 0;

Highlighted Warning:突出警告:

expected a ';'C/C++ (between "(boolean == true)" "and")

and and or are keywords are considered a bit archaic. and and or are 关键字被认为有点过时。 They are technically valid C++ keywords in modern C++ (since C++98 it seems).它们在现代 C++ 中是技术上有效的 C++ 关键字(因为它似乎是 C++98)。 You must be using a very old C++ compiler that was written before they were added to C++.您必须使用一个非常旧的 C++ 编译器,该编译器是在将它们添加到 C++ 之前编写的。 They, and their usage, never took off.它们以及它们的使用从未起飞。 Classical && and ||古典&&|| operators continue to rule the roost and are not going anywhere.运营商继续称雄,不会去任何地方。

Therefore: although it is true that this expression should be logically equivalent to boolean && init() , you might want to consider assigning somewhat higher priority of updating your C++ compiler to something more modern, if that's indeed the reason for the compilation error.因此:尽管这个表达式在逻辑上应该等同于boolean && init() ,但您可能需要考虑分配更高的优先级,将 C++ 编译器更新为更现代的东西,如果这确实是编译错误的原因。

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

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