Originally planned for release last February, C++20 has now received the final technical approval and will be published foreseeably by the end of the year. C++20 will include modules, coroutines, and concepts among its major new features.

Modules introduce a new way to encapsulate code which also aims to make header files unnecessary. The significance of modules cannot be downplayed, wrote Herb Sutter:

This is the first time in about 35 years that C++ has added a new feature where users can define a named encapsulation boundary.

Along with variables, functions, and classes, modules provide a way for programmers to name an entity which hides some kind of implementation. Until now, variables could be used to encapsulate values, functions to encapsulate behaviour, while classes encapsulate state and behaviour. Modules offer a mechanism to encapsulate variables, functions, and classes all together.

That’s a fundamental reason underlying why modules enable further improvements that can now follow on in future C++ evolution.

This is how you can define a simple module exporting a function and its usage in a different file:

// math.cppm
export module mod1;

export int identity(int arg) {
    return arg;
}

// main.cpp

import mod1;

int main() {
  identity(100);
}

#programming languages #c++20 #system programming #development #news

C++20 Is Now Final, C++23 At Starting Blocks
1.25 GEEK