{"id":4676,"date":"2023-08-17T12:35:46","date_gmt":"2023-08-17T10:35:46","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=4676"},"modified":"2023-08-17T12:35:49","modified_gmt":"2023-08-17T10:35:49","slug":"how-to-convert-an-enum-to-string-in-cpp","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/","title":{"rendered":"How to convert an enum to string in C++"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Enumerations are widely used and we often need to convert enum values to strings (typically for recording values in logs) but there is no option at the language level or a utility in the standard library to make it possible. Therefore, developers are usually handcrafting their own solutions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we have the following enum:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">enum person\n{\n  funny, \n  smart, \n  intelligent\n};<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest solution to convert the values to strings is to write a function such as this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const char* to_string(person p)\n{\n  switch(p)\n  {\n    case person::funny:       return \"funny\";\n    case person::smart:       return \"smart\";\n    case person::intelligent: return \"intelligent\";\n  }\n  return \"&lt;none>\"; \/\/ or an empty string\n                   \/\/ or throw an exception\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The problems with this solution include the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You need to explicitly write such a function for every enumeration that you need to be able to convert to string.<\/li><li>Every time you modify the enumeration, typically when adding new values, you must modify the serialization function.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It would be helpful to have a sort of automatic way to generate such functions from the definition of an enumeration. A possible approach is using macros.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A solution for this is explained in the article <a href=\"https:\/\/www.codeproject.com\/Articles\/10500\/Converting-C-enums-to-strings\" target=\"_blank\" rel=\"noopener\" title=\"\">Converting C++ enums to strings<\/a> by Marcos Cardoso. In consists of several steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>create a set of macros for defining the enum<\/li><li>create a set of alternative macro for defining the serialization function<\/li><li>use a macro to switch between these two alternative sets of macros, enabling the generation of the serialization function<\/li><li>include the header containing the enum definition into a special sourse file that defines the switching macro<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">In this approach, we could create a <code>person.h<\/code> header, with the following content:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#if (!defined(PERSON_H) || defined(GENERATE_ENUM_STRINGS))\n\n#if (!defined(GENERATE_ENUM_STRINGS))\n   #define PERSON_H\n#endif\n\n#include \"EnumToString.h\"\n\nBEGIN_ENUM(person)\n{\n   DECL_ENUM_ELEMENT(funny)\n   DECL_ENUM_ELEMENT(smart)\n   DECL_ENUM_ELEMENT(intelligent)\n}\nEND_ENUM(person)\n\n#endif PERSON_H<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The source file <code>EnumToString.cpp<\/code> would contain the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define GENERATE_ENUM_STRINGS  \/\/ Start string generation\n#include \"person.h\"             \n#undef GENERATE_ENUM_STRINGS   \/\/ Stop string generation<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>person<\/code> enum can be used as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;iostream>\n#include \"person.h\"\n\nint main()\n{\n   person p = person::funny;\n   std::cout &lt;&lt; p &lt;&lt; '\\n';\n   std::cout &lt;&lt; GetStringperson(p) &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The benefit of this solution is that the serialization function is generated automatically; you don&#8217;t have to maintain it manually. Whenever a new enumerator is added or an existing one is changed, the changes are reflected immediately in the <code>GetStringX<\/code> function. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, this solution has a significant problem: in practice, many enumerations have explicit values defined for the enumerators (or only for some of them). These macros do not support defining such enumerations. But nothing stops us from expanding them to support both. Therefore, I modified the solution to the following:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The header <code>stringified_enum.h<\/code> contains the following definitions:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#pragma once\n\n#undef DECL_ENUM_ELEMENT\n#undef BEGIN_ENUM\n#undef END_ENUM\n\n#ifndef GENERATE_ENUM_STRINGS\n\n   #define DECLARE_ENUM_ELEMENT(element)  element,\n   #define BEGIN_ENUM(ENUM_NAME, TYPE)    typedef enum tag##ENUM_NAME : TYPE {\n   #define BEGIN_ENUM_INT(ENUM_NAME)      BEGIN_ENUM(ENUM_NAME, int)\n   #define END_ENUM(ENUM_NAME)            } ENUM_NAME; const char* ENUM_NAME##_as_string(enum tag##ENUM_NAME index);\n\n   #define DECLARE_ENUM_ELEMENT_WITH_VALUE(element, value)  element = value,\n   #define BEGIN_ENUM_WITH_VALUES(ENUM_NAME, TYPE)          BEGIN_ENUM(ENUM_NAME, TYPE)\n   #define BEGIN_ENUM_WITH_VALUES_INT(ENUM_NAME)            BEGIN_ENUM(ENUM_NAME, int)\n   #define END_ENUM_WITH_VALUES(ENUM_NAME)                  END_ENUM(ENUM_NAME)\n\n#else\n   #define NO_VALUE                       \"&lt;none>\"\n\n   #define DECLARE_ENUM_ELEMENT(element)     #element,\n\n   #define BEGIN_ENUM(ENUM_NAME, TYPE)       enum tag##ENUM_NAME : TYPE;\\\nconst char* ENUM_NAME##_as_string(enum tag##ENUM_NAME value) {\\\n   std::size_t index = static_cast&lt;std::size_t>(value);\\\n   static const char* s_##ENUM_NAME[] = {\n\n   #define BEGIN_ENUM_INT(ENUM_NAME)         BEGIN_ENUM(ENUM_NAME, int)\n\n   #define END_ENUM(ENUM_NAME)               };\\\n   static const std::size_t s_##ENUM_NAME_len = sizeof(s_##ENUM_NAME)\/sizeof(const char*);\\\n   if(index >=0 &amp;&amp; index &lt; s_##ENUM_NAME_len)\\\n      return s_##ENUM_NAME[index]; \\\n   return NO_VALUE;\\\n}\n\n   #define DECLARE_ENUM_ELEMENT_WITH_VALUE(element, value) {value, #element},\n\n   #define BEGIN_ENUM_WITH_VALUES(ENUM_NAME, TYPE)          enum tag##ENUM_NAME : TYPE;\\\nconst char* ENUM_NAME##_as_string(enum tag##ENUM_NAME value) {\\\n   std::map&lt;TYPE, const char*> sv = {\n\n   #define BEGIN_ENUM_WITH_VALUES_INT(ENUM_NAME)            BEGIN_ENUM_WITH_VALUES(ENUM_NAME, int)\n\n   #define END_ENUM_WITH_VALUES(ENUM_NAME)                  };\\\n   auto it = sv.find(value);\\\n   if (it != sv.end())\\\n      return it->second;\\\n   return NO_VALUE;\\\n}\n\n#endif<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the <code>person.h<\/code> header containing the <code>person<\/code> enum definition changes to the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#if (!defined(PERSON_H) || defined(GENERATE_ENUM_STRINGS))\n\n#if (!defined(GENERATE_ENUM_STRINGS))\n   #define PERSON_H\n#endif\n\n#include \"stringified_enum.h\"\n\nBEGIN_ENUM(person, int)\n   DECLARE_ENUM_ELEMENT(funny)\n   DECLARE_ENUM_ELEMENT(smart)\n   DECLARE_ENUM_ELEMENT(intelligent)\nEND_ENUM(person)\n\n#endif PERSON_H<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There is no significant different than the original macros, except for the name and the curly braces that are now part of the begin and end macros. Also, an underlying type, in this case <code>int<\/code>, is specified explicitly. There is a macro <code>BEGIN_ENUM_INT<\/code> that implicitly uses <code>int<\/code> for this purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, should the enumeration explicitly define values, then the definition of the enum would change to the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">BEGIN_ENUM_WITH_VALUES(person, int)\n   DECLARE_ENUM_ELEMENT_WITH_VALUE(funny, 1)\n   DECLARE_ENUM_ELEMENT_WITH_VALUE(smart, 3)\n   DECLARE_ENUM_ELEMENT_WITH_VALUE(intelligent, 7)\nEND_ENUM_WITH_VALUES(person)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The macros are similar, but they are suffixed with <code>_WITH_VALUE<\/code>\/<code>_WITH_VALUES<\/code>. <code>DECLARE_ENUM_ELEMENT_WITH_VALUE<\/code> requires specifying a value for the enumerator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The source file <code>stringified_enum.cpp<\/code> must be compiled with the rest of the program and contains the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#define GENERATE_ENUM_STRINGS  \/\/ Start string generation\n\n#include &lt;map>\n\n#include \"person.h\"\n\n#undef GENERATE_ENUM_STRINGS   \/\/ Stop string generation <\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Every time you define a new enumeration using this macros, you must include its header in this source file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is a slight change in usage, as the <code>GetStringX<\/code> serialization function is now called <code>x_as_string<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;iostream>\n#include \"person.h\"\n\nint main()\n{\n   person p = person::funny;\n   std::cout &lt;&lt; p &lt;&lt; '\\n';\n   std::cout &lt;&lt; person_as_string(p) &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>But macros are evil! <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We have heard this mantra so many times. The good news, for those of you that want something different, is that solutions that don&#8217;t require macros also exist. Such a solution is the library called <a href=\"https:\/\/github.com\/Neargye\/magic_enum\" target=\"_blank\" rel=\"noopener\" title=\"\">Magic Enum C++<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With this library, you define the enum without any macros, and use <code>magic_enum::enum_name()<\/code> to get the name of an enumerator:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;iostream>\n#include \"magicenum.h\"\n\nenum person\n{\n  funny,\n  smart,\n  intelligent\n};\n\nint main()\n{\n   person p = person::funny;\n   std::cout &lt;&lt; p &lt;&lt; '\\n';\n   std::cout &lt;&lt; magic_enum::enum_name(p) &lt;&lt; '\\n';\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The reason I couldn&#8217;t use Magic Enum was that it defines some global objects. And then, it was used from the initialization of another global in another translation unit. And since there is no order defined for the initialization of globals in different translation units I could not rely on it. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>But global are evil!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. So stop using <code>std::cout<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The macros approach only works for unscoped enums. Magic Enum also works with scoped enums.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enumerations are widely used and we often need to convert enum values to strings (typically for recording values in logs) but there is no option at the language level or a utility in the standard library to make it possible. Therefore, developers are usually handcrafting their own solutions. Let&#8217;s say we have the following enum: &#8230; <a title=\"How to convert an enum to string in C++\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/\" aria-label=\"Read more about How to convert an enum to string in C++\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,645,554],"class_list":["post-4676","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-enums","tag-macros"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Explore different approaches for converting enumerations to strings in C++.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,enums,macros\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"How to convert an enum to string in C++\" \/>\n\t\t<meta property=\"og:description\" content=\"Explore different approaches for converting enumerations to strings in C++.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2023-08-17T10:35:46+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2023-08-17T10:35:49+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"How to convert an enum to string in C++\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Explore different approaches for converting enumerations to strings in C++.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#article\",\"name\":\"How to convert an enum to string in C++\",\"headline\":\"How to convert an enum to string in C++\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2023-08-17T12:35:46+02:00\",\"dateModified\":\"2023-08-17T12:35:49+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":6,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#webpage\"},\"articleSection\":\"C++, C++, enums, macros\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#listItem\",\"name\":\"How to convert an enum to string in C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#listItem\",\"position\":5,\"name\":\"How to convert an enum to string in C++\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/\",\"name\":\"How to convert an enum to string in C++\",\"description\":\"Explore different approaches for converting enumerations to strings in C++.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2023\\\/08\\\/17\\\/how-to-convert-an-enum-to-string-in-cpp\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2023-08-17T12:35:46+02:00\",\"dateModified\":\"2023-08-17T12:35:49+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"How to convert an enum to string in C++","description":"Explore different approaches for converting enumerations to strings in C++.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/","robots":"max-image-preview:large","keywords":"c++,enums,macros","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#article","name":"How to convert an enum to string in C++","headline":"How to convert an enum to string in C++","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2023-08-17T12:35:46+02:00","dateModified":"2023-08-17T12:35:49+02:00","inLanguage":"en-US","commentCount":6,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#webpage"},"articleSection":"C++, C++, enums, macros"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#listItem","name":"How to convert an enum to string in C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#listItem","position":5,"name":"How to convert an enum to string in C++","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/","name":"How to convert an enum to string in C++","description":"Explore different approaches for converting enumerations to strings in C++.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2023-08-17T12:35:46+02:00","dateModified":"2023-08-17T12:35:49+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"How to convert an enum to string in C++","og:description":"Explore different approaches for converting enumerations to strings in C++.","og:url":"https:\/\/mariusbancila.ro\/blog\/2023\/08\/17\/how-to-convert-an-enum-to-string-in-cpp\/","article:published_time":"2023-08-17T10:35:46+00:00","article:modified_time":"2023-08-17T10:35:49+00:00","twitter:card":"summary","twitter:title":"How to convert an enum to string in C++","twitter:description":"Explore different approaches for converting enumerations to strings in C++."},"aioseo_meta_data":{"post_id":"4676","title":null,"description":"Explore different approaches for converting enumerations to strings in C++.","keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2023-08-17 08:59:53","updated":"2025-12-12 07:58:44","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":6,"uagb_excerpt":"Enumerations are widely used and we often need to convert enum values to strings (typically for recording values in logs) but there is no option at the language level or a utility in the standard library to make it possible. Therefore, developers are usually handcrafting their own solutions. Let&#8217;s say we have the following enum:&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">enums<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">macros<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">enums<\/span>","<span class=\"advgb-post-tax-term\">macros<\/span>"]}},"comment_count":"6","relative_dates":{"created":"Posted 3 years ago","modified":"Updated 3 years ago"},"absolute_dates":{"created":"Posted on August 17, 2023","modified":"Updated on August 17, 2023"},"absolute_dates_time":{"created":"Posted on August 17, 2023 12:35 pm","modified":"Updated on August 17, 2023 12:35 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-1dq","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=4676"}],"version-history":[{"count":9,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4676\/revisions"}],"predecessor-version":[{"id":4685,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4676\/revisions\/4685"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=4676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=4676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=4676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}