ReSharper 2021.3 Help

Coding Assistance in C++

Most of ReSharper's coding assistance features are also supported in C++. You can find the detailed information on these features in the corresponding topics of the Coding assistance section. In the main topic of the section, you can also find the feature matrix and check what exactly is supported in C++.

In this topic, you can find some examples of using coding assistance features in C++.

By default, code inspection, quick-fixes, and context actions are available in all solution files. If necessary, you can enable these features in external files referenced from solution with the Enable inspections, quick fixes and context actions in files external to the solution checkbox on the Code Editing | C++ | Inspections page of ReSharper options (Alt+R, O).

When you use custom compiler toolchain, it сan be useful to provide additional compilation properties for better code assistance. To specify the compilation properties, select your project in Solution Explorer and go to the ReSharper section in Properties. You can specify additional include directories, add preprocessor definitions, and set the language standard:

ReSharper C++: additional project properties

The settings will be saved in a .vcxproj.DotSettings file next to the project file. Note that these additional properties do not affect compilation.

Code completion

Automatic and basic completion

In C++ files, you can use automatic and basic Control+Space completion when writing your code. For example, you can quickly add enum members taken from a different namespace:

Automatic completion in C++

The selected completion item displays a popup with syntax-highlighted signature and documentation. If necessary, you can disable this popup by clearing the Show summary checkbox on the Environment | IntelliSense | Completion Appearance page of ReSharper options (Alt+R, O).

Basic completion in C++

Import symbol completion

When a symbol that you want to use is not imported in the current file with the corresponding #include, but it is defined in the standard libraries or elsewhere in your solution, you can press Control+Alt+Space to quickly find this symbol in completion suggestions:

ReSharper: Import symbol completion in C++
Optionally, you can include import symbol suggestions into the suggestion lists of automatic and basic completion by selecting Show import items in basic completion on the Code Editing | C++ | Code Completion page of ReSharper options (Alt+R, O).

Note that this comes with a performance hit (since each time when ReSharper builds a completion list, it has to look at all the symbols in the solution to match them), so it is disabled by default.

Dot/arrow completion helpers

When you call a method, you can always type a dot . or an arrow -> and get all available methods in the completion list. The methods that do not match ./-> are displayed in grey. If you choose such method, the ./-> will be corrected automatically:

Replacing dot with arrow in completion

Generative completion

Generative completion suggestion are also available. For example, when you call a member function declaration on the object...

Generative completion in C++

ReSharper generates the following function:

Generative completion in C++

Postfix completion

Postfix completion in C++ is very much like C# extension methods. If you type a dot . or an arrow -> after an expression, ReSharper will suggest free functions that would accept that expression as the first parameter. When you accept the suggestion, ReSharper will rewrite your code so that the expression is passed as the first argument:

ReSharper C++: Postfix templates

Postfix completion also includes postfix templates which, instead of looking up for suitable methods, let you quickly wrap an expression with frequently used language constructs.

Free functions in code completion

One common C++ coding practice is to prefer non-member non-friend functions to member functions. This is a great way to increase encapsulation and keep class interfaces as minimal as possible. When you type a dot . or an arrow -> after an expression, free functions that accept the expression as the first parameter will be suggested in the completion list after member functions:

ReSharper C++: Free functions in code completion

If you do not want to have free functions in the completion suggestions, you can clear the corresponding checkbox on the Code Editing | C++ | Code Completion page of ReSharper options (Alt+R, O).

Code completion in dependent code

ReSharper automatically uses default template arguments to provide completion suggestions in dependent code. When typing inside a template body, code completion may be unavailable if there is no type information for parameters. However, for template parameters that have default arguments ReSharper will provide completion suggestions based on those default arguments.

ReSharper C++: code completion in dependent code

Code completion for an import item

When Import Symbol completion is enabled, ReSharper’s completion list also suggests items from headers which are not included in the current file. If a class is not included in the current file, the completion list will suggest two options: insert an include directive, or add a forward declaration. A context menu with both options is shown by default, but you can change the default behavior on the Code Editing | C++ | Code Completion page of ReSharper options (Alt+R, O).

ReSharper C++: code completion for an import item

Complete statement

The Complete Statement feature inserts required syntax elements and puts the caret in a position where you can start typing the next statement. It allows you to use the Control+Shift+Enter shortcut instead of having to perform lots of other small actions. For example, this shortcut will automatically insert braces and a semicolon, and then put the caret where you can proceed to write the body:

ReSharper C++: complete statement

Syntax highlighting and tooltips

By default, ReSharper provides extended highlighting of C++ syntax with configurable colors. If necessary, you can disable it on the Code Editing | C++ | Inspections page of ReSharper options. There are 20 identifier types that you can highlight differently. You can change colors and fonts at any time in Visual Studio options (Tools | Options | Environment | Fonts and Colors).

Configuring syntax highlighting for C++ identifiers
Configuring syntax highlighting for C++ identifiers

ReSharper also replaces Visual Studio tooltips for code elements with its own tooltips, which have highlighted syntax, display method and field signatures, formatted XML and Doxygen comments:

ReSharper: Editor tooltip in C++

If necessary, you can disable ReSharper tooltips by clearing the Replace Visual Studio tooltips for C++ checkbox on the Environment | Editor | Visual Studio Features page of ReSharper options.

Parameter information

Whenever you are writing or studying a function call, ReSharper helps you view details on the allowed arguments for all overloads of the function. In a tooltip, you will see all public signatures with parameters and brief description taken from the function's documentation, if any.

As you are typing parameters, ReSharper automatically highlights the next signature compatible with the entered parameters, and grays out inapplicable signatures. To study alternative signatures of an existing function call, set the caret inside the function's parentheses and then press Control+P or choose ReSharper | Edit | Parameter Information from the main menu.

ReSharper C++: Parameter information

The Parameter info tooltip also provides details about members of the aggregate class when performing aggregate initialization, shows information for user-defined binary operators, deleted functions and implicitly generated functions.

On dependent code, it takes template parameter descriptions from documentation comments for template arguments as well as uses default template arguments to provide information about parameters in the dependent code.

ReSharper C++: Parameter information with dependent code

The Parameter info tooltip is configurable on the Environment | IntelliSense | Parameter Info page of ReSharper options.

Rearrange code

Rearrange Code quickly moves code elements around, expands or shrinks the current scope, and more. To rearrange code, press Ctrl+Shift+Alt over the code element or selection that you want to move, then press any arrow key. If you invoke this command without first selecting something, the movable element is selected automatically.

There are two modes for Move left and Move right, which allow you to:

  • Rearrange elements that are normally written in a single line (though they also work if the elements are written in multiple lines). You can use Move left and Move right on elements in a braced initializer list, as well as function or lambda parameters and arguments, and so on.

    ReSharper C++: Rearrange parameters
  • Move a statement or a declaration into the region that directly follows it (Move right) or outside the current region (Move left):

    ReSharper C++: Move a line into the following region

The Move up and Move down commands rearrange elements within a specific scope up and down relative to other elements in this scope, or between neighboring scopes. You can use the Move up and Move down commands on statements and declarations, sections in a switch statement, enumerators, catch sections within a try-catch statement, conditions or branches in if statements, and more.

ReSharper C++: Rearrange if statements

You can also move opening or closing braces up or down to expand or shrink the current compound statement, type, or namespace:

ReSharper C++: Change the namespace scope

The full list of applicable cases:

Move left and Move right

Move up and Move down

  • Statements and declarations out of the current scope and into the following scope

  • Operands in an assignment, binary, fold, or conditional expression

  • Case labels in a switch statement

  • Indices in a subscript expression or an array specifier

  • Function/lambda parameters and arguments

  • Macro parameters and macro call arguments

  • Template parameters and arguments

  • Declarators and structured bindings in a declaration

  • Base types

  • Elements in a braced initializer list

  • Types in an exception specification

  • Doxygen HTML attributes

  • Items in a C++/CLI generic constraint

  • Lambda captures

  • Member initializers

  • Parts of a using declaration

  • Statements and declarations within the surrounding scope

  • Enumerators within an enum declaration

  • Branches in a compound if statement

  • Conditions in nested if statements

  • Requirements within the body of a requires expression

  • Statements from one clause of an if-else, try-catch statement, or from one switch section to another one

  • Opening and closing braces in statements and declarations (greedy brace)

  • Switch sections within a switch statement

  • Catch sections within a try-catch statement

  • Doxygen commands

  • C++/CLI generic constraints

Context Actions

ReSharper provides a set of context actions that target C++ code. You can find the full list of these actions in the Code Editing | C++ | Context actions page of ReSharper Options. If necessary, you can also disable some of the actions using this page.

As soon as a context action becomes available for the current caret position, ReSharper displays the corresponding action indicator Themed icon context action screen gray to the left of the caret. Sometimes however, ReSharper provides several contextually available features for the current caret position. In this case, the action indicator corresponding to the action with the highest priority is shown, and all other actions only appear when you expand the action list by clicking on the action indicator or pressing Alt+Enter Context actions have the lowest priority, therefore, they often appear at the bottom of the action list.

Here are some examples of context actions for C++:

Convert enum to string (generate enum-to-string helper)

This context action generates a helper function for a specific enum that converts its enumerators to corresponding strings.

For example, if you invoke this action on the following enum:

enum class Suit { Diamonds, Hearts, Clubs, Spades };

ReSharper will generate the following function for you:

const char* to_string(Suit e) { switch (e) { case Suit::Diamonds: return "Diamonds"; case Suit::Hearts: return "Hearts"; case Suit::Clubs: return "Clubs"; case Suit::Spades: return "Spades"; default: return "unknown"; } }

If necessary, you can customize generated function by editing the enum_to_string live template that ReSharper uses for generation. For example, you can configure the template to generate stream output operator overload:

std::ostream& operator <<(std::ostream& out, Suit e) { switch (e) { case Suit::Diamonds: return out << "Diamonds"; case Suit::Hearts: return out << "Hearts"; case Suit::Clubs: return out << "Clubs"; case Suit::Spades: return out << "Spades"; default: assert(false); return out << "unknown"; } }

Generate missing case statements

Instead of manually writing all case statements when switching over an enum, you can use the context action that generates all missing case statements:

Generating missing case statements in C++
All you have to do then is to write the required logic for each statement.
Generating missing case statements in C++

Merge nested 'if' statements

ReSharper helps you merge nested 'if' statements with the context action:

Merging nested 'if' statements in C++

Generate implementation

ReSharper helps you automatically create a stub implementation of a function or a method. You can always generate an inline implementation and, if the function is defined in a header file and there is the corresponding source file, you can generate the implementation in the source file.

Generating implementation for a function

Move implementation out of class scope

ReSharper helps you move the implementation of a function or a method from a header to the corresponding source file if it exists. After applying this context action the definition is left in its original place and the implementation is moved to the source file. The editor context is switched to the source file as well.

Moving implementation out of class scope to the source file

You can also use the context action to move all implementations in the current selection.

Document entity

With this context action, you can generate documentation comments for C++ symbols. If necessary, you can customize the comment stub by editing the doc live template that ReSharper uses for generation.

ReSharper: generating documentation comments

This action is also available with the dedicated shortcut Control+OemQuestion and from the menu: ReSharper | Edit | Document.

Highlighting paired items

ReSharper highlights various matching items when you set the caret to one item of the pair:

  • Matching delimiters ((), [], {}, and <>)

  • Matching macros, for example, BEGIN_NAMESPACE/END_NAMESPACE

  • Matching format specifier and argument in printf and boost::format

Last modified: 07 April 2022