Search⌘ K

Submodules and Module Partitions

Learn the difference between submodules and module partitions.

We'll cover the following...

When your module becomes bigger, you want to divide its functionality into manageable components. C++20 modules offer two approaches: submodules and partitions.

Submodules

A module can import modules and then re-export them.

In the following example, module math imports the submodules math.math1 and math.math2.

Javascript (babel-node)
// mathModule.ixx
export module math;
export import math.math1;
export import math.math2;

The expression export import math.math1 imports module math.math1 and re-exports it as part of the module math.

For completeness, here are the modules math.math1 and math.math2. I used a period to separate the module math from its submodules. This period is not necessary.

Javascript (babel-node)
// mathModule1.ixx
export module math.math1;
export int add(int fir, int sec) {
return fir + sec;
}
Javascript (babel-node)
// mathModule2.ixx
export module math.math2;
export {
int mul(int fir, int sec) {
return fir * sec;
}
}

If you look carefully, you recognize a small difference in the export statements in the modules math. While ...