views:

129

answers:

6

Can I declare two variables in a conditional in C++. The compiler gave an error but still I think I should get an opinion:

int main()
{
    double d1 = 0;
    if((double d2 = d1) || (double d3 = 10))
        cout << "wow" << endl;
    return 0;
}
A: 

This is not possible in C++, maybe you were thinking of C# or a newer programming language. No disadvantages to declaring d2 and d3 outside the conditional though.

scripni
A: 

You may want to ask where the C++ standard documentation is.

I don't think that it's a good idea, what you are trying to do is to tell the compiler:

declare d2 put d1 in d2 compare d2 with 0 if it's != 0 jump to the code below else declare d3 put 10 in d3 compare d2 with 0 if it's != 0 jump to the code below else skip the code below

Just declaring d2 and d3 above the conditional could be a good idea, only if you work with limited memory (and do not wish to allocate room for d2 and d3 until this piece of code is reached). It all boils down to what you really intend to do.

kal
+5  A: 

You can only do that for one variable

if(double d2 = d1)
    cout << "wow" << endl;
else 
if(double d3 = 10)
    cout << "wow" << endl;

But i would declare them outside of the conditions. If you are scared about the scope, you can always limit it:

{
  double d2 = d1;
  double d3 = 10;
  if(d2 || d3)
    cout << "wow" << endl;
}

Of course, this evaluates the second initializer even if d2 evaluates to true. But i assume that's not important.

Johannes Schaub - litb
A: 

In C++ you can do something like this:

if (bool b = (a == b)) {
  // Do something with b here...
}

or:

if (double d2 = d1) {
  // ...
}

However, I've never seen this style used in real code. I think there is always a clearer way to write the same thing.

Furthermore, trying to declare multiple variables in the conditional part of an if or while statement is not supported. You will have to resort to nested ifs or else if to get the same semantics.

Example:

if (double d2 = d1) {
  cout << "wow" << endl;
} else if (double d3 = 10) {
  cout << "wow" << endl;
}

This way d3 is only created if d2 evaluates to false.

Whisty
+2  A: 
if((double d2 = d1) || (double d3 = 10))
    cout << d2 + d3 << endl;

the || operator short-circuits, so the d2 + d3 expression references (potentially) uninitialized d3. such a feature would have many ill effects and IMO no benefit, so that's probably why it's not there.

just somebody
_uninitialized_ variables are no problem in C++ (just type `double d3;` and you'll have one). The problem is in _undeclared_ variables, I guess. For example, `double d3; if (double d2 = d1 || d3 = 10)` would have the first sentence (observation) of your answer still be correct, but leave you conclusion (second sentence of your answer) incorrect.
Jasper
@Jasper, having the condition work would be equivalent to allowing a jump over an initialized variable, which is equally disallowed. Note that `double d3;` is allowed, but `string s3;` will never leave the string "uninitialized" - it will call its default constructor. But what do you expect a string that is convertible to `bool` do in such a condition? Will it be initialized by its constructor?
Johannes Schaub - litb
@Johannes, I am fully aware that you are ending up in the magical world of "undefined behaviour" where just about anything can happen, and as such you still shouldn't use the sample I gave, I was just trying to pinpoint the cause of the problem better. I would expect a string that is convertible to a `bool` to convert to false if the string is empty (though I would look that up in the string's documentation just to be sure), but in order to see what I would expect in a `||` situation, I would need an actual example of the situation you are talking about.
Jasper
+1  A: 

There might be something else that is ALSO bothering you here, but there is a problem with how the ||-operator works. I don't remember exactly how it is worded in the standard, but in a || b either your compiler should not evaluate b if a evaluates to true, or your compiler may opt not to do so.

Considering that, in your statement you won't be sure if your variable has been declared - in C++ you can only used declared variables, so d3 will be a pretty useless variable in your example, if you ask me (as you won't be sure if it has been declared).

Jasper
It's declared no matter what. Declarations happen at compile-tile, they aren't determined at run-time. You may skip the initialization, but it will always be declared.
GMan
Wrong. Declaring is indeed done at compile-time. However, because the code is leaving it up to runtime conditions whether it would have to be declared or not, which is something is something that makes no sense, so it spits out an error instead of compiling succesfully. So, basically, yes you can't have a declaration at runtime, which is what you are trying to do here, so it spits out an error...
Jasper