Early Access Program News Tips & Tricks

C++20 Modules Come to ReSharper C++

C++20 modules are a new core language feature. Modules offer a unique way to organize source code with isolated file sets and independently compiled components. After a module is built, other translation units can import it without having to include and process the header files that constitute the interface of the module.

The ReSharper C++ 2022.3 EAP provides experimental support for C++20 modules. First of all, this means that the code analysis engine now understands the semantics of the new compilation model. But of course, there’s also more than that:

You can download the EAP builds from our website or via the Toolbox App.


Let’s take a closer look at how ReSharper C++ helps you work with C++20 modules.

Named modules and header units

ReSharper C++ supports both of the main ways to introduce modules into your codebase: named modules and header units. The modern way is to use named modules. A named module defines its interface through module interface units, which by convention use the .ixx file extension. Additionally, there can be one or several module implementation units.
Module interface unit

After you build a module, the compiler produces a special metadata file that uses the IFC format and describes the public interface of the module. This interface description in the binary format is used by the compiler to import a module into another translation unit.
Importing a module

The main benefit of the new approach is that modules are compiled independently of the code that imports them. This means that preprocessor definitions in the translation unit that imports a module do not affect the imported module. Moreover, there’s no need for the compiler to parse the source code of the module each time it is imported, which has the potential to improve compilation times. We recommend going through the “Named modules tutorial in C++” to learn more about using named modules in Visual C++.

Making the switch to named modules in your projects could be a daunting task. In order to make the transition easier, C++20 provides a way to use existing header files as modules through header units. The idea behind header units is that most header files are sufficiently self-contained so that instead of including them they can be imported as stand-alone modules. More generally, you can think of header units as a modern and standard-supported alternative to precompiled header files.
Header units

An important distinction between a header unit and an include is that header units are not affected by preprocessor definitions outside of them. But similar to including a header file, importing a header unit makes preprocessor definitions from the header unit visible after the import statement. For more information about creating and consuming header units, please refer to the “Build and import header units” walkthrough.

Thus, whichever approach you’d like to experiment with, ReSharper C++ is here to help you.

Syntax highlighting

ReSharper C++ now properly handles new keywords: import, export, and module. You can also use the new ReSharper C++ Module Identifier category in Visual Studio’s Fonts and Colors settings to configure the style of module names.
C++20 modules: Syntax highlighting

Like it does for include directives, ReSharper C++ will warn you about unresolved module names or paths in import declarations.

We’ve added support for module declarations in all basic navigation features. You can now use Go to Declaration from the module name in import directives, navigate to module declarations from File Structure, and search for modules with Go to Symbol.
C++20 modules: Navigation

Code completion

The code completion list now includes items from all the imported modules and header units.
C++20 modules: Code completion

File template for module interface units

If you want to quickly create a module, you can now use a new built-in file template – Module interface unit. To create a new file from a file template, use the Ctrl+Alt+Insert shortcut or select ReSharper | Edit | Create New File from the main menu:
C++20 modules: File templates

Known limitations and roadmap

This is just the beginning of our work on modules support, and there are still some rough edges. The most important limitation is that importing modules directly from binary module interface files is not supported yet. This means that you must have the source of the module included in your solution in order for ReSharper C++ to be able to import it. Additionally, ReSharper C++ expects module interface files to use the conventional .ixx extension.

We are working on introducing modules support in other features for a future release. Our roadmap includes plans to:

  • Allow auto-import features to insert import module directives.
  • Update Find Usages to find all locations where a given module is imported.
  • Extend the Rename refactoring to support renaming modules.
  • Support the std and std.compat standard library modules introduced by P2465.

We hope you can now experiment with this long-awaited C++20 feature in your projects while using ReSharper C++. We would greatly appreciate it if you gave our modules support a try.
Check out the new EAP build and share your feedback with us in the issue tracker or in the comments below. Thank you!


Your ReSharper C++ team
The Drive to Develop

image description