{"id":5013,"date":"2008-04-30T21:15:00","date_gmt":"2008-04-30T21:15:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2008\/04\/30\/enums-macros-unicode-and-token-pasting\/"},"modified":"2019-02-18T18:54:07","modified_gmt":"2019-02-18T18:54:07","slug":"enums-macros-unicode-and-token-pasting","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/enums-macros-unicode-and-token-pasting\/","title":{"rendered":"Enums, Macros, Unicode and Token-Pasting"},"content":{"rendered":"<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Enums, Macros, Unicode and Token-Pasting<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Hi, I am Rocky a developer on the Visual C++ IDE team.<span>&nbsp; <\/span>I would like to discuss the C++ programming technique of creating macro generated enums.<span>&nbsp; <\/span>I recently used this for distinguishing various C++ types such as class, variable, and function.<span>&nbsp; <\/span>Having the list of types in one file makes it easy to add types and allows for many different types of uses.<span>&nbsp; <\/span>The examples I have below mirror uses in code that I have worked with.<span>&nbsp; <\/span>I have also seen this technique used in many places in various other source bases, but I have not seen it discussed much in text books &#8211; so I thought I would highlight this technique.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Consider this enum:<\/font><\/p>\n<p class=\"MsoNormal\"><span>enum<\/span><span> Animal { dog, cat, fish, bird };<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now dog can be used in place of 0.<span>&nbsp; <\/span>You can get compiler enforced type safety that macros do not provide.<span>&nbsp; <\/span>The VS debugger will also show the friendly value of the enum instead of integers.<span>&nbsp; <\/span>However, functions that print out enum values need better formatting.<span>&nbsp; <\/span>This code can help:<\/font><\/p>\n<p class=\"MsoNormal\"><span>wchar_t<\/span><span>* AnimalDiscription[] = { L<span>&#8220;dog&#8221;<\/span>, L<span>&#8220;cat&#8221;<\/span>, L<span>&#8220;fish&#8221;<\/span>, L<span>&#8220;bird&#8221;<\/span> };<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">With this array, debugging code can now print the friendly value of the enum by using the enum to index into the string array.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">With macro generated enums both the enum and the friendly names can be maintained as one entity.<span>&nbsp; <\/span>Consider the file animal.inc:<\/font><\/p>\n<p class=\"MsoNormal\"><span>MYENUM(dog)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>MYENUM(cat)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>MYENUM(fish)<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>MYENUM(bird)<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">And the following C++ code:<\/font><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\">enum<\/span><span lang=\"DE\"> Animal {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\">#define<\/span><span lang=\"DE\"> MYENUM(e) _##e,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#include<\/span><span> <span>&#8220;animal.inc&#8221;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> MYENUM<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>};<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\">wchar_t<\/span><span lang=\"DE\">* AnimalDescription[] = {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\">#define<\/span><span lang=\"DE\"> MYENUM(e) L<span>&#8220;_&#8221;<\/span> L#e,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#include<\/span><span> <span>&#8220;animal.inc&#8221;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> MYENUM<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>};<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\"><\/font>&nbsp;<\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">Now editing animal.inc will update both the enum and the friendly text of the enum.<span>&nbsp; <\/span>In this case I added an underscore in front of the animal names I used before to get the macro to work correctly.<span>&nbsp; <\/span>The token-pasting operator ## cannot be used as the first token.<span>&nbsp; <\/span>The stringizing operator # creates a string from the operator.<span>&nbsp; <\/span>By adding an L right before the stringizing opera\ntor the resulting string is a wide string.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">These macro generated enums can be &ldquo;debugged&rdquo; by using the compiler switch \/EP or \/P.<span>&nbsp; <\/span>This will cause the compiler to output the preprocessor file:<\/font><\/p>\n<p class=\"MsoNormal\"><span>enum<\/span><span> Animal {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>_dog,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>_cat,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>_fish,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>_bird,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>};<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>wchar_t<\/span><span>* AnimalDescription[] = {<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>L<span>&#8220;_&#8221;<\/span> L<span>&#8220;dog&#8221;<\/span>,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>L<span>&#8220;_&#8221;<\/span> L<span>&#8220;cat&#8221;<\/span>,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>L<span>&#8220;_&#8221;<\/span> L<span>&#8220;fish&#8221;<\/span>,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>L<span>&#8220;_&#8221;<\/span> L<span>&#8220;bird&#8221;<\/span>,<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>};<\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">C++ allows for a comma after the last entry of the enum and the array initializer.<\/font><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">This macro string replacement technique can be further expanded to produce code.<span>&nbsp; <\/span>Here is an example of using string replacement to create function prototypes:<\/font><\/p>\n<p class=\"MsoNormal\"><span>#define<\/span><span> MYENUM(e) <span>void<\/span> Order_##e();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#include<\/span><span> <span>&#8220;animal.inc&#8221;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> MYENUM<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font size=\"3\"><font face=\"Calibri\">This expands to:<span><\/p>\n<p><\/span><\/font><\/font><\/p>\n<p class=\"MsoNormal\"><span>void<\/span><span> Order_dog();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>void<\/span><span> Order_cat();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>void<\/span><span> Order_fish();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>void<\/span><span> Order_bird();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">You may wish to do some action based on the kind of animal.<span>&nbsp; <\/span>If you switch on the kind of animal, here is an example of creating case labels and function calls:<\/font><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\">#define<\/span><span lang=\"DE\"> MYENUM(e) <span>case<\/span> _##e:<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span lang=\"DE\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span>Order_##e();<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>break<\/span>;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>#include<\/span><span> <span>&#8220;animal.inc&#8221;<\/p>\n<p><\/span><\/span><\/p>\n<p class=\"MsoNormal\"><span>#undef<\/span><span> MYENUM<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">This expands to:<\/font><\/p>\n<p class=\"MsoNormal\"><span>case<\/span><span> _dog: Order_dog(); <span>break<\/span>;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>case<\/span><span> _cat: Order_cat(); <span>break<\/span>;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>case<\/span><span> _fish: Order_fish(); <span>break<\/span>;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>case<\/span><span> _bird: Order_bird(); <span>break<\/span>;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">\n<p><font face=\"Calibri\" size=\"3\">&nbsp;<\/font><\/p>\n<\/p>\n<p class=\"MsoNormal\"><font face=\"Calibri\" size=\"3\">In this example the function definitions would need to be added for each of the Order_dog(), Order_cat(), etc..<span>&nbsp; <\/span>If you were to add a new animal to animal.inc, you would not need to remember that you would also need to add a new Order_ function definition for this new animal.<span>&nbsp; <\/span>The linker would give you an error reminding you!<\/font><\/p>\n<p><span>Macro string replacement is a powerful tool that can be leveraged to allow for internal data to be stored in one spot.<span>&nbsp; <\/span>Keeping this data in one spot reduces the chances for errors, missed cases or missed matched <span>cases.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Enums, Macros, Unicode and Token-Pasting Hi, I am Rocky a developer on the Visual C++ IDE team.&nbsp; I would like to discuss the C++ programming technique of creating macro generated enums.&nbsp; I recently used this for distinguishing various C++ types such as class, variable, and function.&nbsp; Having the list of types in one file makes [&hellip;]<\/p>\n","protected":false},"author":289,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Enums, Macros, Unicode and Token-Pasting Hi, I am Rocky a developer on the Visual C++ IDE team.&nbsp; I would like to discuss the C++ programming technique of creating macro generated enums.&nbsp; I recently used this for distinguishing various C++ types such as class, variable, and function.&nbsp; Having the list of types in one file makes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/289"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=5013"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/5013\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=5013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=5013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=5013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}