Modules in C++ - what is it and when to use it?

What are modules? Why are they needed and how do they differ from header files? When should I use one, and when should I use the other?

Author: Ivan43, 2020-08-30

1 answers

Https://docs.microsoft.com/ru-ru/cpp/cpp/modules-cpp?view=vs-2019

What are modules?

A module is a set of source code files that are compiled independently of the translation units that import them.

Why are they needed and how do they differ from header files?

  • Modules eliminate or significantly reduce many of the problems associated with the use of header files, and can also reduce compile time.

  • Macros, preprocessor directives, and non-exported names declared in the module are not visible and therefore do not affect the compilation of the conversion record that the module imports.

  • Modules can be imported in any order, without worrying about
    macro overrides.

  • The declarations in the imported record do not participate in overload resolution or name lookup in the imported record. the module.

  • After the module is compiled, the results are stored in a binary file that describes all the exported types, functions, and templates. This file can be processed much faster than the header file, and can be used by the compiler every time the module is imported into the project.

When should I use one, and when should I use the other?

It is recommended that new projects use modules rather than header files, as far as possible. it's possible. For large existing projects under active development, we recommend experimenting with converting legacy headers into modules to determine whether a meaningful reduction in compile time will be obtained.

 8
Author: timur, 2020-09-03 17:39:48