View source code Display the source code in std/typecons.d from which this page was generated on github. 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. Page wiki View or edit the community-maintained wiki page associated with this page.

Template 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.

Arguments

template isBitFlagEnum(E);

Example

enum A
{
    None,
    A = 1<<0,
    B = 1<<1,
    C = 1<<2,
    D = 1<<3,
}

static assert(isBitFlagEnum!A);

enum B
{
    A,
    B,
    C,
    D // D == 3
}

static assert(!isBitFlagEnum!B);

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

Boost License 1.0.

Comments