Convert Macros to Constexpr

Augustin Popa

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.

The macro -> constexpr Quick Fix

Right away, when viewing your code in the editor, you may notice some “…” on #define directives, under certain macros. These “…” 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. Macro suggestion in editor Opening the Quick Actions & Refactorings menu (with Alt + Enter or via the right-click menu) will show a new “Convert macro to constexpr” option. Convert macro to constexpr quick-fix When the option is selected, a preview window appears, summarizing the intended change: Macro->constexpr preview window Once the change is applied, the expression is converted to constexpr in the code editor: Constexpr conversion The feature works for constants, and it also works for basic expressions using function-like macros as well: Function-like macro expressions You may notice that the macro MAX above does not have the “…” 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 “…” 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: Macro converted to template Basically, you can always try to convert a macro to constexpr yourself, just don’t expect it to always work if you do not see a “…”. 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.

Tools > Options Configuration

You can configure the Macro->constexpr feature in Tools > Options Text Editor > C/C++ > View > 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. Tools > Options configuration

Give us your feedback!

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 > Send Feedback > Report A Problem in the IDE.

Posted in C++


Discussion is closed.

Feedback usabilla icon