官术网_书友最值得收藏!

Using modules in C++20

Modules fix header files with annoying include-guard issues. We can now get rid of preprocessor macros. Modules incorporate two keywords, import and export. To use a module, we import it. To declare a module with its exported properties, we use export. Before listing the benefits of using modules, let's look at a simple usage example. The following code declares a module:

export module test;

export int twice(int a) { return a * a; }

The first line declares the module named test. Next, we declared the twice() function and set it to export. This means that we can have functions and other entities that are not exported, thus, they will be private outside of the module. By exporting an entity, we set it public to module users. To use module, we import it as done in the following code:

import test;

int main()
{
twice(21);
}

Modules are a long-awaited feature of C++ that provides better performance in terms of compilation and maintenance. The following features make modules better in the competition with regular header files:

  • A module is imported only once, similar to precompiled headers supported by custom language implementations. This reduces the compile time drastically. Non-exported entities have no effect on the translation unit that imports the module. 
  • Modules allow expressing the logical structure of code by allowing you to select which units should be exported and which should not. Modules can be bundled together into bigger modules.
  • Getting rid of workarounds such as include-guards, described earlier. We can import modules in any order. There are no more concerns for macro redefinitions.

Modules can be used together with header files. We can both import and include headers in the same file, as demonstrated in the following example:

import <iostream>;
#include <vector>

int main()
{
std::vector<int> vec{1, 2, 3};
for (int elem : vec) std::cout << elem;
}

When creating modules, you are free to export entities in the interface file of the module and move the implementations to other files. The logic is the same as in managing .h and .cpp files. 

主站蜘蛛池模板: 阳江市| 开原市| 集安市| 饶平县| 印江| 汉中市| 正安县| 阜宁县| 两当县| 义马市| 清水河县| 北碚区| 根河市| 饶平县| 洪雅县| 裕民县| 晋中市| 吉林市| 东阳市| 苍溪县| 乐亭县| 大邑县| 泾阳县| 博湖县| 庆阳市| 酉阳| 北辰区| 沧州市| 东丽区| 梁河县| 灌阳县| 巫溪县| 恩平市| 肥城市| 运城市| 资溪县| 濮阳市| 定南县| 天镇县| 肇庆市| 甘谷县|