Wild Enum to String
template <auto V> constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { static_assert(!is_enum_v<decltype(V)>, "magic_enum::detail::n requires enum type."); if constexpr (supported<decltype(V)>::value) { #if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); auto name = name_ptr ? str_view{name_ptr, std::char_traits<char>::length(name_ptr)} : str_view{}; #elif defined(__clang__) str_view name; if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { static_assert(always_false_v<decay_t<V>>, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); return str_view{}; } else { name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; name.str_ = __PRETTY_FUNCTION__ + 34; } } if (name.size_ > 22 && name.str_[0] == '(' && name.str_[1] == 'a' && name.str_[16] == ' ' && name.str_[22] == ')') { name.size_ -= 23; name.str_ += 23; } if (name.str_[0] == '(' || name.str_[0] == ' ' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { name = str_view{}; } #elif defined(__GNUC__) auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { static_assert(always_false_v<decay_t<V>>, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); return str_view{}; } else if (name.str_[name.size_ - 1] == ')') { name.size_ -= 55; name.str_ += 54; } else { name.size_ -= 40; name.str_ += 37; } if (name.str_[0] == '(' { name = str_view{};
A dense C++ source-code screenshot shows the internal implementation of enum-to-string conversion in the magic_enum library, with compiler-specific branches for Clang and GCC. The joke is that a simple enum-to-string operation has become wildly complicated, so it is used as a reaction to overengineered code or unexpectedly convoluted implementations. EN: A dense C++ source-code screenshot shows the internal implementation of enum-to-string conversion in the magic_enum library, with compiler-specific branches for Clang and GCC. The joke is that a simple enum-to-string operation has become wildly complicated, so it is used as a reaction to overengineered code or unexpectedly convoluted implementations.
C++ source code / исходный код C++code editor screenshot / скриншот редактора кодаsyntax highlighting / подсветка синтаксисаenum conversion function / функция преобразования enumconditional compilation directives / директивы условной компиляцииClang compiler branch / ветка компилятора ClangGCC compiler branch / ветка компилятора GCCdark background / тёмный фонcolored text / цветной текст
- @ithueti / 16705 2025-08-27
- Type
- image
- Size
- 800×676 · 82.6 KiB
- Mood
- саркастичный, абсурдный
- Language
- en
- Added