enum_names
Function Object DocumentationThe enum_names
function object is designed to efficiently iterate over all possible values of a bounded enumeration, transforming each enumeration value into its string literal representation. This transformation is achieved by utilizing an enum_view
combined with std::views::transform
and a enum_name
function. The function object is tailored for use with bounded enumerations.
std::views::transform
to map enumeration values into string literals.Consider an enumeration representing colors:
enum class Color { Red, Green, Blue, first = Red, last = Blue };
// Assuming enum_name can convert enum values to string names
auto color_names{ enum_names<Color>() };
for (auto color_name : color_names) {
std::cout << color_name << std::endl; // Prints the string name of each color
#if __cplusplus >= 202002L // C++23 or later
auto color_values = enum_enumerations<Color>();
// Create a zip view of the enumeration values and their names
auto zipped_view = std::ranges::zip_view(color_values, color_names);
for(auto [color, name]: zipped_view)
std::cout << "Color enum: " << static_cast<int>(color) << ", Name: " << name << std::endl;
#endif
}