Struct std.typecons.BitFlags
A typesafe structure for storing combinations of enum values.
						
					
				This template defines a simple struct to represent bitwise OR combinations of
enum values. It can be used if all the enum values are integral constants with
a bit count of at most 1, or if the unsafe parameter is explicitly set to
Yes.
This is much safer than using the enum itself to store
the OR combination, which can produce surprising effects like this:
enum E
{
    A = 1 << 0,
    B = 1 << 1
}
E e = EExample
Set values with the | operator and test with &
enum Enum
{
    A = 1 << 0,
}
// A default constructed BitFlags has no value set
immutable BitFlags!Enum flags_empty;
assert(!flags_emptyExample
A default constructed BitFlags has no value set
enum Enum
{
    None,
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2
}
immutable BitFlags!Enum flags_empty;
assert(!(flags_empty & (EnumExample
Binary operations: subtracting and intersecting flags
enum Enum
{
    A = 1 << 0,
    B = 1 << 1,
    C = 1 << 2,
}
immutable BitFlags!Enum flags_AB = BitFlags!Enum(EnumExample
All the binary operators work in their assignment version
enum Enum
{
    A = 1 << 0,
    B = 1 << 1,
}
BitFlags!Enum flags_empty, temp, flags_AB;
flags_AB = EnumExample
Conversion to bool and int
enum Enum
{
    A = 1 << 0,
    B = 1 << 1,
}
BitFlags!Enum flags;
// BitFlags with no value set evaluate to false
assert(!flags);
// BitFlags with at least one value set evaluate to true
flags |= EnumExample
You need to specify the unsafe parameter for enums with custom values
enum UnsafeEnum
{
    A = 1,
    B = 2,
    C = 4,
    BC = B|C
}
static assert(!__traits(compiles, { BitFlags!UnsafeEnum flags; }));
BitFlags!(UnsafeEnum, YesAuthors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara