Avatar ·

How to convert enum to string in modern C++ (C++17/C++20)

Hello colleagues! I'm writing a program in C++17 and encountered a problem: I need to output enum values in a readable form, but standard C++ tools don't provide a direct way to convert an enum to a string. Here's an example of my code: ```cpp enum class Color { Red, Green, Blue, Yellow }; int main() { Color c = Color::Green; // I want to output "Green", but I only get a number std::cout << static_cast(c); // Outputs 1 return 0; } ``` In my real task, the enum contains about 20 values, and I want to use it for logging and user interface. So far I've written a huge switch-case, but it's terrible — every time I add a new value, I need to fix the code in 5 places. I know that C++ has no reflection.