View source code
Display the source code in std/typecons.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Enum member std.typecons.isBitFlagEnum
Detect whether an enum is of integral type and has only "flag" values (i.e. values with a bit count of exactly 1). Additionally, a zero value is allowed for compatibility with enums including a "None" value.
enum isBitFlagEnum(E)
= E .min >= 0 && ()
{
static foreach (immutable flag; EnumMembers!E)
{
{
Base value = flag;
value &= value - 1;
if (value != 0)
return false;
}
}
return true;
}
();
Example
enum A
{
None,
A = 1 << 0,
B = 1 << 1,
C = 1 << 2,
D = 1 << 3,
}
static assert(isBitFlagEnum!A);
Example
Test an enum with default (consecutive) values
enum B
{
A,
B,
C,
D // D == 3
}
static assert(!isBitFlagEnum!B);
Example
Test an enum with non-integral values
enum C: double
{
A = 1 << 0,
B = 1 << 1
}
static assert(!isBitFlagEnum!C);
}
/**
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 = E.A | E.B; // will throw SwitchError final switch (e) { case E.A: return; case E.B: return;
Authors
Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.