简体   繁体   English

Boolean 在 if 语句中的变量声明之前检查

[英]Boolean check before variable declaration in if statement

I have this code:我有这个代码:

#include <iostream>

int function() {
  return 2;
}

int main( void )
{
  int integer = 5;
  if (integer == 5 && int i = function()) {
    std::cout << "true\n";
  } else {
    std::cout << "false\n";
  }
}

It's giving an error:它给出了一个错误:

test.cpp: In function ‘int main()’:
test.cpp:10:23: error: expected primary-expression before ‘int’
   10 |   if (integer == 5 && int i = function()) {
      |                       ^~~
test.cpp:10:22: error: expected ‘)’ before ‘int’
   10 |   if (integer == 5 && int i = function()) {
      |      ~               ^~~~
      |                      )

The order of the parts in the if statement is important to me; if 语句中各部分的顺序对我来说很重要; I only want to call function() if the first check is true.如果第一次检查为真,我只想调用 function() 。 Options I found to fix the error:我发现修复错误的选项:

  int i;
  if (integer == 5 && (i = function())) {

And this, but this does not have the wanted behavior (it always calls function):还有这个,但这没有想要的行为(它总是调用函数):

  if (int i = function() && integer == 5) {

Any other options?还有其他选择吗? I'm also unsure what rule I am violating with my first piece of code.我也不确定我的第一段代码违反了什么规则。 Why isn't it ok?为什么不行?

As an alternative to the other answers, since C++17 you can also declare a variable in the scope of the if in addition to the condition (rather than using a declaration directly in the condition):作为其他答案的替代方案,由于 C++17 除了条件之外,您还可以在if的 scope 中声明一个变量(而不是直接在条件中使用声明):

if(int i; integer == 5 && (i = function()))

You might want to add an initializer to i for a default value.您可能想为i添加一个初始值设定项以获得默认值。

int i;
if (integer == 5 && (i = function())) {

This does what you want, and only calls function() if integer == 5 .这可以满足您的要求,并且仅在integer == 5时才调用function() If it's not 5 it will short circuit and skip the right-hand side.如果不是5它将短路并跳过右侧。

If you want an expression that allows you to control the scope of i, and control whether the function gets called, I'd use the conditional operator, https://en.cppreference.com/w/cpp/language/operator_other , aka ternary operator If you want an expression that allows you to control the scope of i, and control whether the function gets called, I'd use the conditional operator, https://en.cppreference.com/w/cpp/language/operator_other , aka三元运算符

#include <iostream>

int function() {
    return 2;
}

int main(void)
{
    bool boolean_expression = true;
    if (int i = boolean_expression ? function() : 0) {
        std::cout << "true\n";
    }
    else {
        std::cout << "false\n";
    }
}

Or you can just add an additional scope either inside or outside the if statement.或者您可以在 if 语句的内部或外部添加一个额外的 scope。 If these are heavy or RAII objects instead of simple integer and bool then I'd go with an additional if statement or helper function.如果这些是重型或 RAII 对象,而不是简单的 integer 和 bool,那么我将使用 go 和额外的 if 语句或帮助程序 function。

I only want to call function() if the first check is true.如果第一次检查为真,我只想调用function()

Then I would simply use two if s:然后我会简单地使用两个if s:

int integer = 5;
if (integer == 5) {
  int i = function();
  if (i) {
    ...
  }
}

If you really want a single if statement, you could move the declaration of i outside of the statement, like you showed, and then wrap it in a local scope to restrict its use:如果您真的想要一个if语句,您可以将i的声明移到语句之外,就像您展示的那样,然后将其包装在本地 scope 中以限制其使用:

...
{
  int i;
  if (integer == 5 && (i = function())) {
    ...
  }
}
...

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

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