Bug 3999 and 4261

bearophile bearophileHUGS at lycos.com
Sun Sep 5 12:37:44 PDT 2010


Don:
> That's impossible. That would make interfacing to C a nightmare.

I see.


> By the way, enums which consist of flags frequently have values which 
> have more than one bit set. The case { A=1, B=2, C=4, D=8 } is only a 
> special case.

That indeed happens, but from the code that I have seen the most common case is the one with powers of two flags. This is why I think it's useful to add the flagset struct to Phobos, because it avoids/catches some common bugs.


> In the general case, it's not realistic to hope that the compiler could 
> determine which values are valid, and which are not.

Right. But a flagset helps because:
- It gives a very short definition syntax for the common case when flags are power of two.
- In that case it removes the mistakes in power-of-two flag and the missing dummy (0) flag.
- The struct invariant is able to catch few runtime bugs because it tests that the unused bits in the underlying value are always zero.


> But, with my suggestion, simple enums (a list of mutually exclusive 
> values) would become strong enums.

This is better than the current situation. I'd like Walter to give a word on this thread.

Bye,
bearophile


More information about the Digitalmars-d mailing list