-3

I have some code in an old desktop app, which I inheritted from a colegue who left the company. Here is a short similar example of the actual code I have:

#include <iostream>
int getType(int type)
{
    return type;
}

int main()
{
    int variable1 = 30;
    int variable2 = 40;
    int result = (getType(30) == (variable1 || variable2) ? 1 : 2);
    std::cout << result << std::endl;
}

For the result I always receive 2, not 1 as I expected. If I try:

int result = (getType(30) == (variable1) ? 1 : 2)

the result is true.

I can't figure out why this part:

int result = (getType(30) == (variable1 || variable2) ? 1 : 2)

is not true...

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
dudu4
  • 9
  • 5
    That's not how comparing multiple variables with a value works. You want `(getType(30) == variable1) || (getType(30) == variable2)`. – πάντα ῥεῖ May 23 '22 at 16:32
  • [This](https://stackoverflow.com/questions/56922943/how-to-check-if-variable-equal-to-multiple-values) is a C question but it happens to be a case where the same thing applies to both C and C++. There may be a canonical C++ version of this question. – Nathan Pierson May 23 '22 at 16:35
  • you must learn operator precedence first and you can solve such doubts yourself. Happy learning :) – Naseeb Panghal May 23 '22 at 16:38
  • 1
    @NaseebPanghal - nothing to do with operator precedence – pm100 May 23 '22 at 16:39
  • 2
    You seem to be unaware that `(30 || 40)` just means `true`. Or `1` in your case, since you are comparing it to an `int`. Your expression is the same as `getType(30) == 1 ? 1 : 2` – Drew Dormann May 23 '22 at 16:53

2 Answers2

5

In the initializer expression of this declaration

int result = (getType(30) == (variable1 || variable2) ? 1 : 2);

the subexpression (variable1 || variable2) has the type bool and its value is true (variable1 isn't 0 so that is true) that is implicitly converted to the integer value 1 in the expression with the equality operator (getType(30) == (variable1 || variable2) .

From the C++17 Standard (7.6 Integral promotions)

6 A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

So as getType(30) is not equal to 1 the result of the initializer expression is 2.

To get the expected result you should rewrite the declaration like

int result = (getType(30) == variable1 || getType(30) == variable2) ? 1 : 2;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I think perhaps your understanding of the || operator is not quite right. The || operator is the or operator, and it will return a boolean result, false or true. In your code, you are doing this comparison: getType(30) == (variable1 || variable2). The parenthesis get evaluated first because of order of operations and you end up with getType(30) == true, since variable1 has a non-zero value. getType(30) evaluates and returns 30, so the expression is 30 == true. The true value then gets coerced to a 1 because of the rules of c++ and we evalate 30 == 1 which is false.

I think what you are asking for is why this doesn't do what you expect. And I see two problems, the one is that the getType function doesn't actually do what you might expect, it simply returns the variable. I'm not sure what you would expect in c++, but that's definitely a problem. The other expectation is probably that you want to compare the return of getType(30) with both variable1 and variable2 which would require a longer expression. The expression you want is probably getType(30) == variable1 || getType(30) == variable2. This is a common problem because in english we say 'if gettype equals var1 or var2`, but it doesn't translate into code like that unfortunately.

Jeffrey Russell
  • 252
  • 2
  • 9
  • Strictly speaking, `(variable1 || variable2)` is either `false` or `true`, not `0` or `1`. And for these particular values, `(variable1 || variable2)` is `true`, not `false`. But when `true` gets promoted for the comparison with `30`, it gets promoted to `1`, and `30 == 1` is still `false`. – Nathan Pierson May 23 '22 at 16:39
  • Right, thank you, lemme go fix that quickly. – Jeffrey Russell May 23 '22 at 16:40