From bf43110f5a57a95144c55f8855030ba38186d9e8 Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Thu, 18 May 2017 21:07:39 -0400 Subject: [PATCH] Add asOriginalType function for enums --- std/conv.d | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/std/conv.d b/std/conv.d index 7bcf2bfa436..473e7d0f600 100644 --- a/std/conv.d +++ b/std/conv.d @@ -4235,6 +4235,27 @@ private bool isOctalLiteral(const string num) assert(b == 1); } +// asOriginalType +/** +Returns the representation of an enumerated value, i.e. the value converted to +the base type of the enumeration. +*/ +OriginalType!E asOriginalType(E)(E value) if (is(E == enum)) +{ + return value; +} + +/// +unittest +{ + enum A { a = 42 } + static assert(is(typeof(A.a.asOriginalType) == int)); + assert(A.a.asOriginalType == 42); + enum B : double { a = 43 } + static assert(is(typeof(B.a.asOriginalType) == double)); + assert(B.a.asOriginalType == 43); +} + /+ emplaceRef is a package function for phobos internal use. It works like emplace, but takes its argument by ref (as opposed to "by pointer").