Re: [Interest] Flags: Non-intrusive, type-safe bitwise operators for enums

Tobias Loew via Boost <[email protected]>
Newsgroups gmane.comp.lib.boost.devel
Message-ID <[email protected]>
Thanks for the comment.
The code at
https://softwareengineering.stackexchange.com/a/338472/189774 is an
interesting example of a flag implementation, as it also maps enum
values to bit positions. However, while it claims to offer type safety,
it only provides bitwise operator overloads for the given enum.
This seems fine since the features work, but there is a problem:
erroneous code still compiles. For example, when two different enums are
accidentally mixed:

enum e {e0, e1, e2, e3};
enum f {f0, f1, f2, f3};
if ((e0 | e3) & e1) {...} // uses bitflag class -> evaluates to false
if ((e0 | f3) & e1) {...} // use integral promotion and built-in
operators -> evaluates to true

Of course, this problem only exists for unscoped enums, but flag-like
enums are usually defined that way.
My Flags library dedicates a lot of code to prohibiting operations that
should not compile; for an enabled enum, only bitwise operators with
itself or its complement type are allowed.
_______________________________________________
Boost mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://lists.boost.org/mailman3/lists/boost.lists.boost.org/
Archived at: https://lists.boost.org/archives/list/[email protected]/message/CHM2F6AQGI5XXVI5GNC26QL2OQXQFEKI/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.