getEnumIndex

Enum index that corresponds of the list returned by std.traits.EnumMembers.

@safe pure nothrow @nogc
bool
getEnumIndex
(
T
)
(
const T value
,
ref uint index
)
if (
is(T == enum)
)

Return Value

Type: bool

enum member position index in the enum definition that corresponds the value.

Examples

import std.meta: AliasSeq;

enum Common { a, b, c }
enum Reversed { a = 1, b = 0, c = -1 }
enum Shifted { a = -4, b, c }
enum Small { a = -4, b, c = 10 }
enum Big { a = -4, b, c = 1000 }
enum InverseBool { True = true, False = false }
enum FP : float { a = -4, b, c }
enum S : string { a = "а", b = "б", c = "ц" }

uint index = -1;
foreach (E; AliasSeq!(Common, Reversed, Shifted, Small, Big, FP, S))
{
    assert(getEnumIndex(E.a, index) && index == 0);
    assert(getEnumIndex(E.b, index) && index == 1);
    assert(getEnumIndex(E.c, index) && index == 2);
}

assert(getEnumIndex(InverseBool.True, index) && index == 0);
assert(getEnumIndex(InverseBool.False, index) && index == 1);

Meta