ReSharper 2024.1 Help

C++/CLI Support

C++/CLI is a set of C++ language extensions by Microsoft designed for easy interoperability between managed and native code. It includes generics, value and ref classes, handles, tracking references, interfaces, and other syntactic additions.

Besides the main set of code analysis, navigation and search, coding assistance, and other features you’re used to with C++, ReSharper provides additional support for C++/CLI-specific cases.

Search and navigation

Since C++/CLI is a bridge between the languages, ReSharper provides cross-language navigation features to improve interoperability:

Go to Derived Symbols also works for get/set functions inside C++/CLI properties and add/remove functions inside C++/CLI events.

Go to Derived in C++/CLI

Code analysis

ReSharper includes a set of inspections for C++/CLI-specific language constructions:

  • You’ll get a warning if there is missing virtual specifier for an interface member, or it does not have public accessibility.

    Method must be public and virtual
  • ReSharper inspects the way you allocate memory and suggests a quick-fix to replace gcnew with new when the target type is not valid. It also works for replacing new with gcnew.

    Cannot use gcnew on a native type
  • ReSharper warns you when you try to use the C++ final specifier on a managed class or interface, with a quick-fix to make it sealed instead.

    Replace final with sealed

There are also a few helpful quick-fixes for C++/CLI. For example, the safe_cast operator in C++/CLI is especially useful when downcasting from a base class to a derived one: safe_cast performs a dynamic check and throws InvalidCastException if the conversion fails. You can use a quick-fix to convert a C-style cast to a safe_cast.

C-style cast used instead of a C++ cast

Inlay hints

In addition to parameter name and type name hints, ReSharper provides type conversion hints for C++/CLI boxing conversions, which result in an allocation on the managed heap. Such hints for standard conversions are disabled by default, but you can enable them on the Environment | Inlay Hints | C++ | Type Conversion Hints page of ReSharper options Alt+R, O.

C++/CLI boxing conversions

Formatting style

Additional formatting settings allow you to configure style for C++/CLI generic constraints: select the preferred indentation, add spaces where needed, and force line breaks (Code Editing | C++ | Formatting Style).

generic <class ItemType> where ItemType:IItem ref class Stack {};
generic <class ItemType> where ItemType : IItem ref class Stack {};

Postfix completion

Some C++ postfix templates are specially designed to produce C++/CLI code when invoked in its context.

  • Use .gcnew to produce instantiation expression for a type.

  • Use .safe_cast to surround expression with .safe_cast.

Known issues and limitations

Last modified: 26 May 2024