std::exchange
can be used to do exactly this:
bool b = std::exchange(isTrue, !isTrue);
It's equivalent to:
bool b = (isTrue := !isTrue)
Where :=
is a magical assignment operator that returns the old value rather than the freshly-assigned value.
If you instead would like b
to have the old value of isTrue
and set isTrue
to false
, as the discussion in the comments suggests, you can do this with:
bool b = std::exchange(isTrue, false);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…