## Modules
Legacy `includes` system form `C` language finally was replaced in C++20 by modules. `include` is actually 50 years old!
New keywords:
* Export
* Import
___
## Less code in binary
```C++
#include
int main() {
std::cout << "Hello World!\n";
}
```
```C++
g++ -std=c++2b -E main.cpp | wc -c
929065
```
```C++
import ;
int main() {
std::cout << "Hello Modular World!\n";
}
```
```C++
g++ -std=c++2b -fmodules-ts main.cpp | wc -c
239
```
___
## Export
Modules provide better encapsulation because you can export only those methods, which you mark as exported. The rest of the functions stay hidden outside the file.
```C++
// calculator.cc
export module Calculator;
export auto add(auto x, auto y) {
return x + y;
}
export auto substract(auto x, auto y) {
return x - y;
}
export namespace Advanced {
auto factorial(auto x) {
decltype(x) res = 1;
for (int i = 2 ; i <= x ; ++i) {
res *= i
}
return res;
}
}
void this_function_will_not_be_exported() {}
// main.cc
import
;
import Calculator;
int main() {
std::cout << "10 + 20 = " << add(10, 20) << '\n';
std::cout << "40 - 60 = " << substract(40, 60) << '\n';
std::cout << "5! = " << Advanced::factorial(5) << '\n';
// this_function_will_not_be_exported -> compile error
}
```
___
## spread modules across multiple files
Only one file can be `interface` and `export` module. Other files can write implementations.
```C++
// interface.cc
export module calculator
export {
auto add(auto x, auto y);
auto substract(auto x, auto y);
}
// add.cc
module calculator
auto add(auto x, auto y) {
return x + y;
}
// substract.cc
module calculator
auto substract(auto x, auto y) {
return x - y;
}
// main.cc
import ;
import calculator;
int main() {
std::cout << "10 + 20 = " << add(10, 20) << '\n';
std::cout << "40 - 60 = " << substract(40, 60) << '\n';
}
```
___
## Compiler and Cmake supoort
Unfortunately when we want to build binary with modules using g++ or clang even when we have the newest version (on 17.05.2022 there is g++ 13 and clang 15) we still have partial support for modules. It will takes a few years before we will start using this in customer projects. Currently, even CMake(3.23.1) Didn't fully support modules: (https://gitlab.kitware.com/cmake/cmake/-/issues/18355).