{"id":19735,"date":"2018-06-26T13:42:02","date_gmt":"2018-06-26T20:42:02","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/?p=19735"},"modified":"2019-02-18T17:47:52","modified_gmt":"2019-02-18T17:47:52","slug":"convert-macros-to-constexpr","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/convert-macros-to-constexpr\/","title":{"rendered":"Convert Macros to Constexpr"},"content":{"rendered":"<p>Visual Studio 2017 version 15.8 is currently available in preview. Today, Preview 3 has been released, and it comes with several features that improve the developer productivity experience. One key theme in 15.8 is code modernization, and macros are a key target for that. In 15.8 Preview 1, we announced the ability to expand macros in Quick Info tooltips, and now, for Preview 3, we are happy to announce a way to convert them to modern C++ constexpr expressions. The new preview includes a quick fix, accessible from the editor window, that identifies macros that can be converted to constexpr, and offers the option to perform the conversion, as a way to clean up and modernize your code. This feature (like editor features in general) is configurable and can be turned on\/off as needed.<\/p>\n<h4>The macro -&gt; constexpr Quick Fix<\/h4>\n<p>Right away, when viewing your code in the editor, you may notice some \u201c\u2026\u201d on #define directives, under certain macros. These \u201c\u2026\u201d are called Suggestions, and they are a separate category from errors (red squiggles; for most severe issues), and warnings (green squiggles; for moderately severe issues). A Suggestion covers low severity code issues. \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/110.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/110.png\" alt=\"Macro suggestion in editor\" width=\"580\" height=\"250\" class=\"aligncenter size-full wp-image-19745\" \/><\/a>\nOpening the Quick Actions &amp; Refactorings menu (with Alt + Enter or via the right-click menu) will show a new \u201cConvert macro to constexpr\u201d option. \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/24.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/24.png\" alt=\"Convert macro to constexpr quick-fix\" width=\"684\" height=\"312\" class=\"aligncenter size-full wp-image-19755\" \/><\/a>\nWhen the option is selected, a preview window appears, summarizing the intended change: \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/34.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/34.png\" alt=\"Macro-&gt;constexpr preview window\" width=\"562\" height=\"450\" class=\"aligncenter size-full wp-image-19765\" \/><\/a>\nOnce the change is applied, the expression is converted to constexpr in the code editor: \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/42.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/42.png\" alt=\"Constexpr conversion\" width=\"499\" height=\"41\" class=\"size-full wp-image-19775\" \/><\/a>\nThe feature works for constants, and it also works for basic expressions using function-like macros as well: \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/51.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/51.png\" alt=\"Function-like macro expressions\" width=\"632\" height=\"460\" class=\"aligncenter size-full wp-image-19785\" \/><\/a>\nYou may notice that the macro MAX above does not have the \u201c\u2026\u201d under it. For function-like macros, we do not run a full preprocess to guarantee that the attempted conversion will be successful, to maintain stable IDE performance. Since we only want to show the Suggestion when we can guarantee that the conversion makes sense, we elect not to show the \u201c\u2026\u201d indicator. However, you can still find the option to convert in the lightbulb menu, and we then fully process the macro when you click Apply in the preview window. In this case, this macro is converted to the following template: \n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/61.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/61.png\" alt=\"Macro converted to template\" width=\"974\" height=\"74\" class=\"size-full wp-image-19795\" \/><\/a>\nBasically, you can always try to convert a macro to constexpr yourself, just don\u2019t expect it to always work if you do not see a \u201c\u2026\u201d. Not all macros are actually constexpr-able, since there are a wide range of macros that exhibit all sorts of behaviors that are unrelated to constants and expressions. <\/p>\n<h4>Tools &gt; Options Configuration<\/h4>\n<p>You can configure the Macro-&gt;constexpr feature in Tools &gt; Options Text Editor &gt; C\/C++ &gt; View &gt; Macros Convertible to constexpr. There, you can choose whether to display instances of it as Suggestions (default behavior), Warnings (green squiggles), Errors (build-breaking red squiggles), or None (to hide the editor indicator altogether) depending on your preference.\n<a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/71.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/9\/2019\/02\/71.png\" alt=\"Tools &gt; Options configuration\" width=\"775\" height=\"434\" class=\"aligncenter size-full wp-image-19805\" \/><\/a><\/p>\n<h4>Give us your feedback!<\/h4>\n<p>This is our first release for this feature. We appreciate any feedback you may have on how we can make it better in the comments below. If you run into any bugs, please let us know via Help &gt; Send Feedback &gt; Report A Problem in the IDE. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 2017 version 15.8 is currently available in preview. Today, Preview 3 has been released, and it comes with several features that improve the developer productivity experience. One key theme in 15.8 is code modernization, and macros are a key target for that. In 15.8 Preview 1, we announced the ability to expand macros [&hellip;]<\/p>\n","protected":false},"author":1063,"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-19735","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus"],"acf":[],"blog_post_summary":"<p>Visual Studio 2017 version 15.8 is currently available in preview. Today, Preview 3 has been released, and it comes with several features that improve the developer productivity experience. One key theme in 15.8 is code modernization, and macros are a key target for that. In 15.8 Preview 1, we announced the ability to expand macros [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/19735","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\/1063"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=19735"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/19735\/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=19735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=19735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=19735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}