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:
Find Usages and Go To Declaration work between C++/CLI and C#.
When looking for derived symbols from C++/CLI code, ReSharper will find symbols in C# code in addition to symbols in C++/CLI.
Go to Base Symbols and Go to Derived Symbols work on imported CLR entities.
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](https://resources.jetbrains.com/help/img/dotnet/2024.2/cpp_cli_navigation.png)
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 havepublic
accessibility.ReSharper inspects the way you allocate memory and suggests a quick-fix to replace
gcnew
withnew
when the target type is not valid. It also works for replacingnew
withgcnew
.ReSharper warns you when you try to use the C++
final
specifier on a managed class or interface, with a quick-fix to make itsealed
instead.
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 C-style cast used instead of a C++ cast](https://resources.jetbrains.com/help/img/dotnet/2024.2/cpp_cli_safe_cast.png)
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 page of ReSharper options Alt+R, O.
![C++/CLI boxing conversions C++/CLI boxing conversions](https://resources.jetbrains.com/help/img/dotnet/2024.2/cpp_cli_inlay_hints.png)
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 (
).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
.