Veda
Veda

Reputation: 2063

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; I only want to call function() if the first check is true. 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?

Upvotes: 0

Views: 322

Answers (4)

Remy Lebeau
Remy Lebeau

Reputation: 595887

I only want to call function() if the first check is true.

Then I would simply use two ifs:

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:

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

Upvotes: 0

user17732522
user17732522

Reputation: 76668

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):

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

You might want to add an initializer to i for a default value.

Upvotes: 2

Kenny Ostrom
Kenny Ostrom

Reputation: 5871

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

#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 these are heavy or RAII objects instead of simple integer and bool then I'd go with an additional if statement or helper function.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361595

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

This does what you want, and only calls function() if integer == 5. If it's not 5 it will short circuit and skip the right-hand side.

Upvotes: 0

Related Questions