With the introduction of C++20, modules provide a modern alternative to traditional header files, enabling better encapsulation and faster compilation times. Instead of including headers, you define modules that can be imported by other translation units, thus promoting better code organization and reducing dependencies.
Here’s a simple example of how to define and use a module in C++20:
// my_module.cppm
export module my_module;
export int add(int a, int b) {
return a + b;
}
// main.cpp
import my_module;
#include
int main() {
std::cout << "Sum: " << add(5, 3) << std::endl;
return 0;
}
In this example, we define a module named my_module
that contains a simple function add()
. We can then import this module in our main program and use its functionality directly.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?