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

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. 

主站蜘蛛池模板: 岐山县| 杭锦后旗| 南昌市| 西昌市| 阿拉善右旗| 齐齐哈尔市| 乌拉特前旗| 荔波县| 丰镇市| 自治县| 红桥区| 威海市| 子洲县| 玉门市| 龙胜| 伊吾县| 凉山| 北宁市| 新疆| 平乐县| 洛南县| 灵台县| 江川县| 高陵县| 甘德县| 乐都县| 舞钢市| 屯留县| 蓬溪县| 旌德县| 天气| 宜宾市| 九龙城区| 乾安县| 武功县| 大宁县| 保靖县| 秀山| 高平市| 潜山县| 乐至县|