ReSharper 2024.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.

Note that built-in code cleanup profiles do not include some C++ specific tasks, so you need to create a new custom profile to enable them.

Managing and applying code formatting rules

An important aspect of code style is how to format the code, that is, how to use whitespaces, tabs, and line breaks to arrange code elements, whether and how to use tabs for indents, whether and how to wrap long lines, and so on.

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 and Clang-Format files.

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 settings layer or to a configuration file in the .editorconfig or .clang-format format.

For more information, refer to Learn formatting rules from existing code.

Naming style

ReSharper helps you define, control, and apply desired naming styles for symbols in your code. There is 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 capitalization of compound words, underscores, suffixes, prefixes, and so on.

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 Alt+R, O, 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.

Customize naming ruleset

  1. Select ReSharper | Options from the main menu or press Alt+R O, then choose Code Editing | C++ | Naming Style on the left.

  2. At the top-right corner of the page, use the Reset to Scheme selector to choose a naming style scheme to base your ruleset on.

  3. You will see a list of identifiers and other items for which naming rules are configured.

  4. To change the rule for an item, select it and click Edit ThemedIcon.EditorOptionsPage.Screen.(Gray).png or just double-click it. To add a rule for a non-listed item, click Add ThemedIcon.AddedParameter.Screen.(Gray).png.

  5. In the dialog that opens, you can configure the new or the modified rule:

    ReSharper: Configuring a C++ naming rule
  6. In the left part of the dialog, you can specify the rule's description — it helps understand, which kinds of identifiers the rule applies to. Other controls in the left part let you restrict the rule's applicability to specific identifiers by their kind, specifiers, and accessibility.

    Note that specifiers are configured with tri-state checkboxes: a tick means only with this specifier, clear means only without this specifier, and a square means with and without this specifier. The illustration above shows a rule that will apply to public static functions.

  7. In the right part of the dialog, you can configure styles for the rule. You need at least one style. If you would like to allow other styles for this rule, click Add ThemedIcon.AddedParameter.Screen.(Gray).png. When there are several styles for a single rule, ReSharper does not report code style violation if a corresponding symbol name matches at least one of these styles. Otherwise, ReSharper detects a code style violation and suggests renaming the symbol according to the default style (the first style in the list). To change the default style, use Move Up ThemedIcon.Up.Screen.(Gray).png and Move Down ThemedIcon.Down.Screen.(Gray).png buttons.

  8. To configure a specific style, select it in the list and configure prefix, suffix, and choose a naming style.

  9. To remove an unnecessary style, select it in the list and click Remove ThemedIcon.Delete.Screen.(Gray).png.

  10. If necessary, you can disable reporting code style violations associated with this rule, while still allowing ReSharper to detect other naming rule violations. To do so, clear the Enable inspections checkbox.

  11. If the rule editor was opened from the Options dialog, click Set to close the Edit Rule Settings dialog.

  12. When you are back on the Naming Style settings page, use Move Up ThemedIcon.Up.Screen.(Gray).png and Move Down ThemedIcon.Down.Screen.(Gray).png buttons to change the priority of rules — if several rules match a specific identifier kind, the rule higher in the list applies. This means that rules with more restrictions should be placed higher than rules with less restrictions. For example, both rules Functions, and Public static functions apply to a public static function, so the Public static functions rule is placed higher in the list:

    ReSharper: Naming style settings for C++
  13. Click Save in the Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer by choosing this layer from the Save To selector. For more information, see manage and share resharper settings.

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 page of ReSharper options Alt+R, O will be ignored when you're working on a UE4 project. If you still want to use 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.

Sorting of includes

ReSharper allows you to sort #include directives, rearrange the existing groups and create new ones separated by blank lines.

When the caret is on an #include directive, there are three context actions in the Sort #include directives submenu:

Sort #include directives

Sort and regroup #include directives

This action merges all #include groups into one, then sorts it and splits into new groups according to the configured settings.

ReSharper C++: Sort and regroup #include directives

Sort each #include group separately

This action sorts #include directives inside each group independently and does not change the existing grouping.

ReSharper C++: Sort each #include group separately

Sort only current #include group

This action sorts the group that the caret is on.

You can configure the ordering rules on the Code Editing | C++ | Order of #includes page of ReSharper options Alt+R, O.

ReSharper supports importing the sorting settings from a .clang-format file. When Read code style form .clang-format style is checked on the Code Editing | General Formatter Style page of ReSharper options Alt+R, O, the IncludeBlocks, IncludeCategories, IncludeIsMainRegex, and SortIncludes settings from your .clang-format file will be used to sort the #include directives in the same way that clang-format does.

Sorting of includes can also be applied with code cleanup.

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 source files or use it to replace existing file headers.

You can configure file header style either in ReSharper options or in .editorconfig files and then add it to new files created with file templates and insert or replace file headers in existing files using code cleanup.

To make sure that files created and edited by different team members have the same header style, share file header settings with your team.

Last modified: 26 May 2024