ReSharper 2019.1 Help

Code Style Assistance in C++

ReSharper provides a lot of features for keeping your code neat and clean. Being tightly interwoven with the majority of other ReSharper's features, they help you produce code and change existing codebases according to the specific code style. The code style, which includes naming standards, formatting rules, and many other tiny aspects can be configured to a very detailed level and shared across your team.

Similarly to other languages, all C++ code style preferences can be applied in the desired scope with a single command using either Fix in scope or Code cleanup.

Managing and applying code formatting rules

An important aspect of code style is how to format the code, i.e., how to use whitespaces and blank lines to arrange and separate code blocks, whether and how to use tabs for indents, whether and how to wrap long lines, etc.

The extensive set of ReSharper code formatting rules has a default configuration that takes into account default Visual Studio formatting options as well as numerous best practices. You can configure every detail of formatting rules and enforce the rules in your code. These rules are applied when ReSharper produces new code with code completion and code generation features, applies code templates and performs refactorings. The formatting rules can also be applied to the existing code in the current selection, current file, or in a larger scope up to the entire solution.

ReSharper stores formatting preferences using the mechanism of shared settings. You can configure formatting rules in options pages under the Code Editing | C++ | Formatting Style group. You can also store and share formatting settings in the EditorConfig of Clang-Format files.

As an alternative to digging through options pages, you can select a block of code, press Alt+Enter and choose Format | Configure to configure formatting rules applicable to this block, observing the changes right in this block.

Another alternative to tweaking individual formatting preferences is to learn formatting rules from an existing code sample, which can be a selected block, the current file, a set of files, one or more projects, or the entire solution. ReSharper will analyze the selected sample and list formatting rules that differ from your current settings. You will then be able to review the detected rules, change them as required and save them to the desired.
For more information, see Using formatting rules from existing code

Naming style

ReSharper helps you define, control, and apply naming style for symbols in your code. Naming style is implemented as a set of rules, each of which targets specific identifiers with the set of constraints. Each rule can have one or more associated styles that define suffixes, prefixes, capitalization of compound words, etc.

These rules are taken into account when ReSharper produces new code with code completion and code generation features, applies code templates and performs refactorings. ReSharper also helps you detect and fix violations of naming rules. If necessary, the automatic checkup of naming rules can be configured or disabled.

On the Code Editing | C++ | Naming Style page of ReSharper options, you can reset your ruleset to one of the predefined naming style schemes (STL, Google, LLVM, CamelCase, Microsoft). You can use these schemes as is or configure your own ruleset based on a predefined one.

If you're working with a UE4 project, ReSharper will automatically apply a naming ruleset corresponding to the Unreal Engine coding standard, which is not configurable. This means that the rules configured on the Code Editing | C++ | Naming Style settings page will be ignored when you're working on a UE4 project. If you still want to used your configured ruleset instead of the Unreal Engine coding standard, you can disable the corresponding inspection:

ReSharper: Applying UE4 naming conventions

Arranging cv-qualifiers

ReSharper helps you arrange cv-qualifiers in generated code. On the Code Editing | C++ | Code Style page of ReSharper options, you can specify whether to put cv-qualifiers before or after the type specifier as well as the order of the const and volatile modifiers.

Include directive style

On the Code Editing | C++ | Code Style page of ReSharper options, you can specify whether to use forward or backward slash as a path separator in include directives in generated code. You can also specify whether or not to generate forward declaration in the header file.

Default pointer initializer style

You can specify 0, nullptr or NULL as the preferred initializer for pointer initializers, for example: const char *foo = nullptr. You can change your preference on the Code Editing | C++ | Code Style page of ReSharper options.

File header style

Using file headers for copyright notices and other identifying messages is a common practice. ReSharper allows you to configure the default header text and automatically insert it into code files of your solution.

The configured file header could be inserted in new files if you create them using file templates. To do so, you should add a template parameter and associate the 'File header' macro with it.

If you want to apply the file header to existing files, you can do it with the code cleanup.

Last modified: 21 May 2019

See Also