1

I had a boolean that needs to be flipped each time its used, since the code was rather simple every other line was me flipping the boolean. I fiddled around a little and came up with this (even more simplified example)

#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    bool flippy = 0;

    cout << (flippy = !flippy) << "\n";
    cout << (flippy = !flippy) << "\n";
    cout << (flippy = !flippy) << "\n";
    cout << (flippy = !flippy) << "\n";

    system("PAUSE");
    return 0;
}

It produces 1 0 1 0 as expected but looks a bit odd, is this valid use of the language?

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26
  • Why would it be invalid ? If it was, then the language wouldn't allow it. – Unda Jul 03 '14 at 09:18
  • As long `flippy` is properly initialized, I can't see anything wrong doing so. – πάντα ῥεῖ Jul 03 '14 at 09:18
  • 4
    Yes, but if you don't understand what is going on, it is very easy to slip into Undefined Behaviour from here. I'd recommend making the assignment it's own line. – BoBTFish Jul 03 '14 at 09:19
  • 1
    @Unda The C++ standard defines quite a few constructs which have undefined behavior and do not require a diagnostic (since it is almost impossible to give one). – pmr Jul 03 '14 at 09:19
  • 1
    @Unda Read up on [Undefined Behaviour](http://blog.regehr.org/archives/213) – BoBTFish Jul 03 '14 at 09:19
  • @Unda Not true, we have a lot of stuff in the [undefined behavior tag](https://stackoverflow.com/questions/tagged/undefined-behavior) that says otherwise. – Shafik Yaghmour Jul 03 '14 at 09:21
  • @haccks It's not being modified more than once? (And `C++` doesn't have sequence points any more, but as the behaviour doesn't really change, and we don't know what compiler the asker is using, that's somewhat irrelevant). – BoBTFish Jul 03 '14 at 09:23
  • @haccks Please [read this answer](http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points/4183735#4183735) – Rapptz Jul 03 '14 at 09:24
  • what i red out of it was "5) i = ++i + 1 ; //well defined behaviour" and "4) i = i++ + 1 ; //Undefined Behaviour" still confused :x – Jake Freelander Jul 03 '14 at 09:26

2 Answers2

5

Yes it is valid. It is not exactlly something I would call good style.

Jakub
  • 583
  • 3
  • 8
0

Your code is of course valid. You can also use a bitset as the following example.

#include <iostream>
#include <bitset>

using namespace std;

int main()
{
    bitset<1> myBit;

    cout << myBit << endl;

    cout << myBit.flip() << endl;
    cout << myBit.flip() << endl;
    cout << myBit.flip() << endl;
}
Dakorn
  • 883
  • 6
  • 11
  • -1000000 for use of `bitset`! You *can* do this, but why on Earth would you? What possible advantage could it give? And in [ideone](http://ideone.com/5ytOc5), it is 4 times bigger than a `bool`! – BoBTFish Jul 03 '14 at 09:27
  • I have to aggree with @BoBTFish. `std::bitset` has it's uses , but not this one. – πάντα ῥεῖ Jul 03 '14 at 09:29
  • @KarliRaudsepp I'd still prefer you write your own `flip` on a `bool`: `bool flip(bool &b) { return (b = !b); }` (or return by reference, depending on what you want). Then write `std::cout << flip(flippy) << '\n';` for every line. – BoBTFish Jul 03 '14 at 09:34