On Mon, Oct 14, 2019 at 11:27:54AM -0700, Jonathan Tan wrote:
> > Jonathan Tan <jonathanta...@google.com> writes:
> > 
> > >> > Also, I have a slight preference for putting "= 02" on the BLAME_COPY
> > >> > line but that is not necessary.
> > >> 
> > >> That is absolutely necessary; it is not like "we do not care what
> > >> exact value _COPY gets; it can be any value as long as it is _MOVE
> > >> plus 1", as these are used in set of bits (and again, I do not think
> > >> it is such a brilliant idea to use enum for such a purpose).
> > >
> > > Good point.
> > 
> > Doesn't that also show that enums are not quite a good fit for set
> > of bits (i.e. 1<<n)?
> 
> Well, I agree that it could be better, but with the C language as we
> have it now, I still slightly prefer an enum to a list of #define. Both
> ways, we still have to manually enter values for each flag, but with
> enum, we get better debugger display (at least in gdb) and in the
> function declaration and definition, we can put a specific type (instead
> of "unsigned" or "int").

And then with any halfway decent editor (i.e. with ctags support) we
can jump to the definition of that enum, see all the constants and
read the comments.  A proper IDE might even display all that as a
tooltip when hovering over.

> gdb supports the notion that a few people use
> enums this way too, but if we decide as a project to not use enums in
> this way, that's fine too.

In a curious coincidence, today we celebrate the 12th anniversary of
Git starting using an enum as a set of bitflags.  Yay! :)

> For what it's worth, I tried doing a search
> online, but most of the results I got was for C# (where it is
> recommended - they have a "[Flags]" attribute for enums), so maybe I am
> indeed in the minority.

I looked around among the various sources I have lying around, and
found enums used as a set of bitflags in libgit2, linux, gcc, glibc,
curl, vim, dash (but not Bash :).


Anyway.  Using an enum for a set of related bitflags has some very
practical benefits.  OTOH, I'm still not sure what its drawbacks are,
especially what those drawbacks are that outweight the benefits.

Reply via email to