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.
This commit is contained in:
parent
6064be33e5
commit
bd3c351f1f
@ -23,6 +23,7 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#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 <typename T>
|
||||
constexpr typename std::underlying_type<T>::type enumValue(T e) {
|
||||
return static_cast<typename std::underlying_type<T>::type>(e);
|
||||
}
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
#endif // _SG_INLINES_H
|
||||
|
Loading…
Reference in New Issue
Block a user