Use code MD50 on Firmware Engineer's Arsenal to get a 50% off! end in:

-

Modules in C++ 20


Modules are a new feature in C++20 that allow you to group related code together into a single unit. This can make your code more modular and easier to understand.

Introduction

Modules are a new feature in C++20 that allow you to group related code together into a single unit. This can make your code more modular and easier to understand. Modules are also more efficient than traditional header files, because they only need to be compiled once, even if they are used by multiple source files.

In this article, we will discuss the following topics:

  • What are modules in C++?
  • How to use modules in C++
  • Compilation steps with g++
  • Examples of using modules
  • Conclusion

What are modules in C++?

Modules are a new feature in C++20 that allow you to group related code together into a single unit. This can make your code more modular and easier to understand. Modules are also more efficient than traditional header files, because they only need to be compiled once, even if they are used by multiple source files.

A module is a self-contained unit of code that can be imported into other source files. A module can contain functions, classes, variables, and other declarations. Modules are also namespace-aware, which means that you can use the import keyword to import a module and its namespace into your code.

How to use modules in C++

To use modules in C++, you need to add the module keyword to the beginning of your source file. You also need to specify the name of the module, which is the same as the filename. For example, the following code defines a module called math:

export module math;
export int add(int x, int y){
    return x + y;
}
export namespace math {  // this function will be called with namespace
    auto sub(auto x, auto y) -> decltype(x-y) {
        return x - y;
    }
}
void print() {} // this function will not be exported
math.cpp

The module keyword tells the compiler that this is a module file. The export keyword tells the compiler that the function or namespace is to be exported from the module. The import keyword tells the compiler that this file imports a module.

Once you have defined a module, you can use it in other source files by importing the module. For example, the following code imports the math module and then calls the add() function:

Code snippet

import <iostream>;
import math;
int main() {
    std::cout << "3+6 = " <<  add(3,6) << "\n";
    std::cout << "6-2 = " << math::sub(6,2) << "\n";
}
main.cpp

The import keyword tells the compiler that this file imports the math module. The add() function is exported from the math module, so it can be called without any namespace qualification. The sub() function is also exported from the math module, but it is in a namespace, so it must be called with the math:: namespace qualification.

Compilation steps with g++

To compile and run the code in this article, you would use the following commands:


g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
g++ -std=c++20 -fmodules-ts -c math.cpp
g++ -std=c++20 -fmodules-ts -c main.cpp
g++ -std=c++20 main.o math.o -o main
Compilation Commands

The -std=c++20 flag tells GCC to compile the code using the C++20 standard. The -fmodules-ts flag tells GCC to compile the code as modules. The -xc++-system-header iostream flag tells GCC to compile the iostream header file as a system header.

The main.o and math.o files are the object files that are generated by compiling the main.cpp and math.cpp files, respectively. The -o main flag tells GCC to create an executable file called main from the main.o and math.o files.

./main

3+6 = 9
6-2 = 4
output

Examples of using modules

Here are some examples of how you can use modules in C++:

  1. You can use modules to organize your code into logical units.
  2. You can use modules to share code between different

Conclusion

Modules are a powerful new feature in C++ that can make your code more modular and efficient. If you are new to modules, I encourage you to check out the C++ Modules tutorial for more information.


Read more: