From bd3c351f1f3cadb68ef4028d386739ac93a25fa6 Mon Sep 17 00:00:00 2001 From: Florent Rougon Date: Sat, 18 Nov 2017 22:27:47 +0100 Subject: [PATCH] Add function template simgear::enumValue() to simgear/sg_inlines.h This function template is useful to get the value of a member of a scoped enumeration, without having to hardcode its type while casting it. --- simgear/sg_inlines.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/simgear/sg_inlines.h b/simgear/sg_inlines.h index b21304d8..4c4b4ad5 100644 --- a/simgear/sg_inlines.h +++ b/simgear/sg_inlines.h @@ -23,6 +23,7 @@ // // $Id$ +#include #include #ifndef _SG_INLINES_H @@ -123,6 +124,17 @@ void noexceptSwap(T& a, T& b) noexcept swap(a, b); } +// Cast an enum value to its underlying type (useful with scoped enumerations). +// +// Example: enum class MyEnum { first = 1, second }; +// auto e = MyEnum::second; +// std::string msg = "MyEnum::second is " + +// std::to_string(simgear::enumValue(e)); +template +constexpr typename std::underlying_type::type enumValue(T e) { + return static_cast::type>(e); +} + } // of namespace simgear #endif // _SG_INLINES_H