3

Code C++17

#include <iostream>

int main()
{
    bool n=-1;
    n++;    // Not compiles
    n=n+3;  // This compiles
    return 0;
}

Output

ISO C++17 does not allow incrementing expression of type bool

So I am not understanding the significance of allowing addition but not increment.

  • 3
    Does this answer your question? https://stackoverflow.com/a/3450592 https://stackoverflow.com/a/17179339 – kiner_shah Nov 07 '21 at 07:49
  • Can you include error text, please? – Vad Sim Nov 07 '21 at 07:52
  • 1
    `source>:6:6: error: ISO C++17 does not allow incrementing expression of type bool [-Wincrement-bool]` - live - https://godbolt.org/z/7vdzshc6q _"...The operand expr of a built-in prefix increment or decrement operator must be a modifiable (non-const) lvalue of __non-boolean (since C++17)__ ..."_ https://en.cppreference.com/w/cpp/language/operator_incdec Standard Change - [Remove Deprecated operator++(bool) - P0002R1](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0002r1.html) – Richard Critten Nov 07 '21 at 07:59
  • It seems a little inconsistent to me. My guess is it's an oversight, but I would be interested to hear if there really is a reason for allowing one and not the other. – Galik Nov 07 '21 at 08:02
  • @kiner_shah No that didn't answer my question. Regarding 2nd comment yes you are right but I am asking why they allow addition operation then ? – Forrest Gump Nov 07 '21 at 08:03
  • 1
    @RichardCritten All you are really doing is reiterating the situation, that incrementing is prohibited whereas other arithmetic is allowed. The question is why is there such an inconsistency? Why aren't both prohibited? – Galik Nov 07 '21 at 08:05
  • 1
    @Galik If integral arithmetic on `bool` would be prohibited it would hurt me much (or, at least, a bit). It allows nice little tricks like `(a > b) - (a < b)` to compare integrals spaceship-operator-like without the danger of int overflow U.B. It's also nice for bit fiddling. – Scheff's Cat Nov 07 '21 at 08:12
  • I suspect the real question is why is `bool` not promoted to `int` when *incrementing* when it is promoted in other arithmetic operations. – Galik Nov 07 '21 at 08:12
  • @ForrestGump -- Assign `n+3` to an `int` instead of `n`. What is the resulting value? My guess is that it has to do with integer promotion rules. – PaulMcKenzie Nov 07 '21 at 08:13
  • @Galik Maybe, because the pre-increment requires a reference (for argument _and_ return). – Scheff's Cat Nov 07 '21 at 08:13
  • @Galik, Boolean arithmetic would require a whole different investigation into the impact on existing code vs. how much of an actual bother it is (i.e., can it lead to subtle bugs?) As alluded to, I suspect the impact is far greater. Some such code comes from old C where there were no booleans in the first place. – chris Nov 07 '21 at 08:23
  • @Galik No my question is that I can understand why increment and decrement is not operate. As on boolean type variable incrementing and decrementing is insignificant and what's the purpose of incrementing boolean type variable anyhow ? I get that. Might be what I am getting is wrong I don't know. But on other hand it is allow adding on boolean data type variables, so what's the point of that ? – Forrest Gump Nov 07 '21 at 08:36
  • 2
    @Galik to answer why, you would need to be present (or have the records) of the 1998 C++ Standards Committee _"...The ++ operator for bool was deprecated in the original 1998 C++ standard,..."_ I feel this is a fruitless exercise and turns this question into Opinion Based. – Richard Critten Nov 07 '21 at 08:48
  • @Galik, FYI, I checked D&E's section on `bool` and it doesn't appear to mention anything about this. – chris Nov 07 '21 at 09:13
  • 4
    I suspect @Scheff'sCat is correct here. With `++b` if you promote the `bool b` to `int` you end up with an r-value temporary. You can't increment `r-values`. So in order for `++b` to have ever worked it must have been a `bool` operation, not a promotion to an arithmetic value. Now, that `bool` operation has been banned. But promotions remain legal so arithmetic that uses promoted values is fine. – Galik Nov 07 '21 at 09:38
  • @Galik I got it. Thanks for elaboration. – Forrest Gump Nov 07 '21 at 11:31

1 Answers1

3

As you can see in section 5.3.2 of standard draft N4296, this capability has been deprecated

The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated)

Please note that the expression n=n+3; is not a unary statement, and it's not something that we could call deprecated. If we call it deprecated, the first problem is that there will be no implicit conversion from bool to int, for example. Since they don't know what you want to do with a not unary statement, it is reasonable that the below code gives you the output 2 for i(Deprecating this is not acceptable)

bool b = true;
int i = 1;
i = i + b;

In your example, what happens is implicit conversion from bool->int->bool

bool n=-1;
n++;    // It's a unary statement(so based on the draft, it would not compile)
n=n+3;  // This compiles (type(n+3) is now int, and then int is converted to bool)

Why unary increment is deprecated?

I use Galik's comment for completing this answer:

With ++b if you promote the bool b to int you end up with an r-value temporary. You can't increment r-values. So in order for ++b to have ever worked it must have been a bool operation, not a promotion to an arithmetic value. Now, that bool operation has been banned. But promotions remain legal so arithmetic that uses promoted values is fine.

Saeed Masoomi
  • 1,703
  • 1
  • 19
  • 33