Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
cppx source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
CppCon 2017
CppCon 2018
p1240r1
p1240r2 trunk
p2320 trunk
Options
Source code
#include <algorithm> #include <iostream> #include <string> #include <string_view> #include <type_traits> #include <utility> #include <vector> #include <experimental/meta> using namespace std::experimental; template <typename Enum> constexpr std::underlying_type_t<Enum> to_underlying(Enum e) noexcept { return static_cast<std::underlying_type_t<Enum>>(e); } template <typename Int> using enum_value_name_pair = std::pair<Int, std::string_view>; template <typename Int> struct enum_value_comparator { bool operator()(const enum_value_name_pair<Int>& lhs, const enum_value_name_pair<Int>& rhs) const { return lhs.first < rhs.first; } bool operator()(Int lhs, const enum_value_name_pair<Int>& rhs) const { return lhs < rhs.first; } bool operator()(const enum_value_name_pair<Int>& lhs, Int rhs) const { return lhs.first < rhs; } }; template <typename Int, typename Iterator> auto sort_uniq_first(Iterator first, Iterator last) { std::vector<std::decay_t<decltype(*first)>> result{first, last}; std::stable_sort(result.begin(), result.end(), [](const auto& lhs, const auto& rhs) { return lhs.first < rhs.first; }); auto end = std::unique(result.begin(), result.end(), [](const auto& lhs, const auto& rhs) { return lhs.first == rhs.first; }); result.erase(end, result.end()); return result; } template <typename Enum, typename Map> auto get_enum_value_name_map(const Map& map) { return sort_uniq_first<std::underlying_type_t<Enum>>( map.begin(), map.end()); } template <typename Enum> auto get_raw_enum_value_name_map() { std::vector< enum_value_name_pair<std::underlying_type_t<Enum>>> result; template for (constexpr auto e : meta::members_of(^Enum)) { result.emplace_back(to_underlying([:e:]), meta::name_of(e)); } return result; } template <typename Enum> const std::vector< enum_value_name_pair<std::underlying_type_t<Enum>>>& get_enum_value_name_map() { static auto result = get_enum_value_name_map<Enum>( get_raw_enum_value_name_map<Enum>()); return result; } template <typename Int, typename Iterator> Iterator find_enum_value(Int value, Iterator first, Iterator last) { enum_value_comparator<Int> compare; Iterator it = std::lower_bound(first, last, value, compare); if (it != last && !compare(value, *it)) { return it; } return last; } template <typename Enum> requires std::is_enum_v<Enum> std::string to_string(Enum value) { const auto& map = get_enum_value_name_map<Enum>(); auto it = find_enum_value<std::underlying_type_t<Enum>>( to_underlying(value), map.begin(), map.end()); if (it != map.end()) { return std::string(it->second); } return std::string("(") + meta::name_of(^Enum) + ")" + std::to_string(to_underlying(value)); } enum class Color { red = 1, green, blue }; int main() { std::cout << to_string(Color::red) << '\n'; std::cout << to_string(Color{9}) << '\n'; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree