{"id":2111,"date":"2022-03-20T13:22:00","date_gmt":"2022-03-20T12:22:00","guid":{"rendered":"http:\/\/www.nuonsoft.com\/blog\/?p=2111"},"modified":"2022-03-20T13:22:02","modified_gmt":"2022-03-20T12:22:02","slug":"c20-enum-class-and-using-declarations","status":"publish","type":"post","link":"https:\/\/www.nuonsoft.com\/blog\/2022\/03\/20\/c20-enum-class-and-using-declarations\/","title":{"rendered":"C++20: enum class and using Declarations"},"content":{"rendered":"\n<p>C++11 has given use strongly-typed enumeration types which are recommended to be used over the old style enumeration types. The problem with the latter is that the enumerators of such old-style enumeration types are automatically exported into the enclosing scope. This can cause naming collisions with names already defined in the enclosing scope. This is also the reason why enumerators of such types are often prefixed with some label to try to make sure they are unique. For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nenum Color { ColorRed, ColorGreen, ColorBlue };\n<\/pre><\/div>\n\n\n<p>The strongly-typed enumeration types from C++11 do not automatically export their enumerators to the enclosing scope.<\/p>\n\n\n\n<p>Let&#8217;s look at an example. The following defines and uses a strongly-type enumeration type called <strong>Color<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nenum class Color { Red, Green, Blue };\nColor someColor = Color::Green;\n<\/pre><\/div>\n\n\n<p>To use the enumerators of the <strong>Color <\/strong>enumeration type, you need to fully qualify them with <strong>Color::<\/strong>. This can become a bit cumbersome if you, for example, need to have a <strong>switch<\/strong> statement on the different enumerators:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nswitch (someColor) {\n    case Color::Red:\n        \/\/ ...\n        break;\n    case Color::Green:\n        \/\/ ...\n        break;\n    case Color::Blue:\n        \/\/ ...\n        break;\n}\n<\/pre><\/div>\n\n\n<p>Since C++20, you can use a <strong>using <\/strong>declaration to avoid having to fully qualify all the different enumerators in the <strong>switch <\/strong>cases:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [2,4,7,10]; title: ; notranslate\" title=\"\">\nswitch (someColor) {\n    using enum Color;\n\n    case Red:\n        \/\/ ...\n        break;\n    case Green:\n        \/\/ ...\n        break;\n    case Blue:\n        \/\/ ...\n        break;\n}\n<\/pre><\/div>\n\n\n<p>Of course, it is recommended to have the scope of the <strong>using<\/strong> declaration as small as possible, otherwise you again introduce the risk of having naming collisions. That&#8217;s why the <strong>using<\/strong> declaration in the earlier example is inside the scope of the <strong>switch<\/strong> statement.<\/p>\n\n\n\n<p>My book,\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/amzn.to\/2ZDHCCu\" target=\"_blank\">Professional C++, 5th Edition<\/a>, explains all new C++20 features, and much more.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++11 has given use strongly-typed enumeration types which are recommended to be used over the old style enumeration types. The problem with the latter is that the enumerators of such old-style enumeration types are automatically exported into the enclosing scope. This can cause naming collisions with names already defined in the enclosing scope. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[242],"class_list":["post-2111","post","type-post","status-publish","format-standard","hentry","category-c","tag-c20"],"_links":{"self":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2111"}],"version-history":[{"count":5,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2111\/revisions"}],"predecessor-version":[{"id":2116,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/posts\/2111\/revisions\/2116"}],"wp:attachment":[{"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nuonsoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}