RFC: BitMask helper ADT

Saleem Abdulrasool compnerd at compnerd.org
Tue Jun 23 16:12:47 PDT 2015


Hello,

While looking at something unrelated (yay yak-shaving!) ... it came up that
it may be convenient to actually re-use enumerations as bit masks in
certain cases (assuming that the values are appropriately disjoint).

The thing is that enum classes are becoming more prevalent, and they are
not simply usable in logical operations without casting.  The "solution" to
this is replicate the enumeration.  The helper helps avoid the extensive
casting by wrapping up the underlying type into a helper that simply
provides the overloads for the normal (logical) operations.  It would also
allow the re-use of the enumeration rather than duplicate the values.

The implementation presented here should work with MSVC 2013, clang and
GCC.  It is more of a proof of concept, that this is possible.  This thread
is meant to raise the question of: is this a good idea?  Do we want to just
use unsigned for bit masks instead?

(A thank you goes out to Aaron for a discussion on this idea and help with
some of the nastier pieces of the metaprogramming here).

One point that I know will most definitely be brought up: the implicit
boolean conversion.  Yes, its implicit, and yes that was an explicit
choice.  The problem is that the use of logical operators as conditions is
pretty extensive, so this conversion operator tends to be involved quite
often, and would be needed to keep the idiomatic use in place.

-- 
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150623/073434b6/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-ADT-introduce-BitMask-T.patch
Type: application/octet-stream
Size: 4670 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150623/073434b6/attachment.obj>


More information about the llvm-commits mailing list