布尔运算符++和 - [英] bool operator ++ and --
问题描述
今天,当写一些的Visual C ++ code我所遇到的事情已经让我感到惊讶。看来C ++的支持++(递增)为布尔,但不是 - (递减)。这只是这一个随机决定的,或者有这背后某种原因?
这编译:
静态HMODULE HMOD = NULL;
静态布尔一旦= FALSE;
如果(!一旦++)
HMOD =调用LoadLibrary(XXX);
这不:
静态HMODULE HMOD = NULL;
静态布尔一旦=真;
如果(once--)
HMOD =调用LoadLibrary(XXX);
据来自使用整数值作为布尔的历史。
如果 X
是 INT
,但我使用它作为一个布尔值按如果(X)...
然后递增将意味着操作之前,无论它的真值,就会有一个真值真正
后(除溢出)。
然而,这是不可能的predict结果 -
给出的知识仅 X $ C $的真值C>,因为它可能导致
假
(如果积分值为1)或真正
(如果积分值是其他任何东西 - 特别是这包括0 假
]和2个或更多[真正
])
因此,作为一个速记 ++
工作,和 -
没有
++
上允许的bool与此兼容,但在标准的使用是德precated。
这假定我只使用 X
作为一个布尔值,这意味着溢出不可能发生,直到我做了 ++
往往不足以引起它自己的溢出。即使字符所使用的类型和 CHAR_BITS
一些低像5,这是在此之前的32倍不工作的话(这仍然论据足够它是一个不好的做法,我不是卫冕的做法,只是解释为什么它的工作原理)的32位 INT
当然,我们将不得不使用 ++
2 ^ 32倍之前,这是一个问题。随着 -
虽然它只会造成假
如果我开始为值1 真正
,或者开始与0和使用 ++
precisely过一次。
这是不同的,如果我们开始与这仅仅是在0以下几个事实上,在这种我们可能需要的情况下的值 ++
来导致假
终值,如:
INT X = -5;
而(+ X)
DoSomething的(X);
然而,这个例子对待 X
为 INT
随处可见,除了有条件的,所以它等价于:
INT X = -5;
而(++到X!= 0)
DoSomething的(X);
这是只使用不同的 X
作为一个布尔值。
Today while writing some Visual C++ code I have come across something which has surprised me. It seems C++ supports ++ (increment) for bool, but not -- (decrement). It this just a random decision, or there is some reason behind this?
This compiles:
static HMODULE hMod = NULL;
static bool once = false;
if (!once++)
hMod = LoadLibrary("xxx");
This does not:
static HMODULE hMod = NULL;
static bool once = true;
if (once--)
hMod = LoadLibrary("xxx");
It comes from the history of using integer values as booleans.
If x
is an int
, but I am using it as a boolean as per if(x)...
then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true
after it (barring overflow).
However, it's impossible to predict the result of --
given knowledge only of the truth value of x
, as it could result in false
(if the integral value is 1) or true
(if the integral value is anything else - notably this includes 0 [false
] and 2 or more [true
]).
So as a short-hand ++
worked, and --
didn't.
++
is allowed on bools for compatibility with this, but its use is deprecated in the standard.
This assumes that I only use x
as an boolean, meaning that overflow can't happen until I've done ++
often enough to cause an overflow on it's own. Even with char as the type used and CHAR_BITS
something low like 5, that's 32 times before this doesn't work any more (that's still argument enough for it being a bad practice, I'm not defending the practice, just explaining why it works) for a 32-bit int
we of course would have to use ++
2^32 times before this is an issue. With --
though it will only result in false
if I started with a value of 1 for true
, or started with 0 and used ++
precisely once before.
This is different if we start with a value that is just a few below 0. Indeed, in such a case we might want ++
to result in the false
value eventually such as in:
int x = -5;
while(++x)
doSomething(x);
However, this example treats x
as an int
everywhere except the conditional, so it's equivalent to:
int x = -5;
while(++x != 0)
doSomething(x);
Which is different to only using x
as a boolean.
这篇关于布尔运算符++和 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!