Make Member Function const and Global Function Static in Visual Studio

Make Member Function const and Global Function Static in Visual Studio

Mryam Girmay

We are delighted to announce that Visual Studio Preview now includes the “Make Member Function Const” and “Make Global Function Static” features. To check out these features, make sure you update to the latest version of Visual Studio Preview. 

Make Member Function Const

Visual Studio now generates hints to mark member functions as const when a member function doesn’t modify the object’s state. This feature identifies cases where a member function performs actions that could be accomplished through the object’s public interface using a const pointer.

We have made this process easier for you by adding a suggestion that allows you to make a member function const. When a member function can be made const, there will be a suggestion (indicated by three dots) below the function. Hovering over them will inform you that “the member function can be made const,” and you can click on the light bulb to make the member const.

Image make const

When you make a member function const, the change is applied to all the files that have the member function. You can preview all the changed files by selecting the “Preview all changed files” link.

Make function const in multi file GIF

“Make Member Function Const” is on by default, and it is set as suggestion. The setting can be found and configured by navigating to Tools > Options > Text Editor > C/C++ > Code Style > Linter. 

Make Member Function Const Setting

 

Make Function Static

In Visual Studio Preview, you’ll now receive hints to mark global functions as static. When encountering a global function that doesn’t have a forward declaration in a header file, you will be able to make the function static effortlessly. We already have a feature that detects global functions and suggests creating forward declarations, and now we have added an option to mark them as static. This feature works smoothly with modern C++ projects, as it doesn’t provide suggestions to make anonymous namespaces static. Static global functions are accessible within the translation unit where they are defined.

When there is a global function that doesn’t have a forward declaration, it will have a suggestion. Hovering over the suggestion will reveal a screwdriver icon, signaling that the function can be made static. You can complete the process by clicking the screwdriver icon and selecting “Make this function static.”

Image make function static

You can configure this feature’s setting in Tools > Options > Text Editor > C/C++ > IntelliSense. While the default tag is set as a suggestion, you have the flexibility to customize the severity level to none, warning, or error.

Make global function static setting.

 

Send us your feedback

To experience this exciting new feature, make sure to download the latest version of Visual Studio Preview. Your feedback is of utmost importance to us, as it guides the enhancement of Visual Studio. Please share your insights in the comments section, the developer community, or on Twitter (@VisualC). Alternatively, you can also reach out to us via email at visualcpp@microsoft.com.