61

Convert Macros to Constexpr

 6 years ago
source link: https://www.tuicool.com/articles/hit/BbayYjM
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.

2Q7bYbR.png!web

Opening the Quick Actions & Refactorings menu (with Alt + Enter or via the right-click menu) will show a new “Convert macro to constexpr” option.

2QFne2m.png!web

When the option is selected, a preview window appears, summarizing the intended change:

eaq6BbU.png!web

Once the change is applied, the expression is converted to constexpr in the code editor:

RnyIbmu.png!web

The feature works for constants, and it also works for basic expressions using function-like macros as well:

yAzIj26.png!web

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:

zqyEjaa.png!web

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.

BJfeMvf.png!web

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK