Smartenum
Macros for declaring enums in C++ that include to_string conversion and looping through all enum values
Install / Use
/learn @therocode/SmartenumREADME
smartenum
Macros for declaring enums in C++ that include to_string conversion and looping through all enum values
Purpose
Sometimes, you find the need of converting enum values to strings mostly for debugging/logging purposes. This can easily be achieved by maintaining a map that maps the enum values to strings. Doing this manually however is a source for errors since one might forget to update it when enum values are added, or just make a mistake when copy-pasting. I am also a fan of reducing boilerplating. These factors made me create this header file.
Features
- Automatic generation of to_string functions for converting enum values to string representations
- Provides a way to loop through all values of an enum
- Supports both enums and enum classes
- Self contained header file
Usage
Smart enums are very easy to use. The code below shows it all.
#include "smartenum.hpp"
//declares a normal enum 'Day'
smart_enum(Day, MONDAY, TUESDAY, THURSDAY = 4, FRIDAY);
//declares an enum class 'Dish'. Newline works as expected
smart_enum_class(Dish, PANCAKE,
OMELETTE = 2,
WAFFLES = 42);
int main()
{
//with normal enums, the to_string function is prepended with <ENUM_NAME>_ to avoid collisions
std::cout << Day_to_string(MONDAY) << " " << Day_to_string(TUESDAY) << " " << Day_to_string(THURSDAY) << " " << Day_to_string(FRIDAY) << "\n";
//with enum classes, the to_string function is used as is
std::cout << to_string(Dish::PANCAKE) << " " << to_string(Dish::OMELETTE) << " " << to_string(Dish::WAFFLES) << "\n";
//enum classes also support ostream conversion directly
std::cout << Dish::PANCAKE << " " << Dish::OMELETTE << " " << Dish::WAFFLES << "\n\n";
//The macros define a way to access the enum values as a list (std::vector)
std::cout << "The days of the week are:\n";
for(Day day : Day_list)
{
std::cout << Day_to_string(day) << " ";
}
std::cout << "\n\n";
std::cout << "The dishes we have are:\n";
for(Dish dish : Dish_list)
{
std::cout << to_string(dish) << " ";
}
std::cout << "\n\n";
}
The above code prints:
MONDAY TUESDAY THURSDAY FRIDAY
PANCAKE OMELETTE WAFFLES
PANCAKE OMELETTE WAFFLES
The days of the week are:
MONDAY TUESDAY THURSDAY FRIDAY
The dishes we have are:
PANCAKE OMELETTE WAFFLES
Feedback
Any suggestions on how to improve the existing codes or features to add are welcome!
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
