Why do we need modules

I have been using C++ since 1996. Yes, C++ is an old language but I always found it the best language to u to achieve any goal, including goals that are typically associated with other languages. I use C++ for automation (and not Python) for example. In our book, we also try to include unconventional abilities and capabilities done in C++, showing how C++ can be use for almost anything.

One of the biggest problems with C++ is its header file system.

You have to include a lot of headers to use even the simplest libraries, and this can lead to long compile times, conflicts, and other issues.

That’s where C++ 20 modules come in – a new feature that aims to make our lives as C++ developers easier. But what are modules, and why do we need them? Let’s find out.

What are modules

Modules are a new way of organizing C++ code that allows us to encapsulate code within a module and export only the symbols we want to make public. This means that we can avoid naming conflicts, manage dependencies between different parts of our codebase, and reduce compilation times. Module are among the topics covered in our book Learning C++ 

How Do We Use Modules?

To use modules, we first need to create a module declaration file (.ixx) and a module implementation file (.cxx). The declaration file defines the interface of the module, while the implementation file contains the implementation details. We can then import the module in our main source file using the import statement.

module example;
export int add(int a, int b) { return a + b; }

We can then import this module in our main source file like this:

import example;
int main() 
{ 
    int sum = add(2, 3); 
    return 0; 
}

As you can see, using modules is much cleaner and easier than using headers, and it can save us a lot of time and headaches.

Why Do We Need Modules?

There are several reasons why we need modules in C++. First, modules can help us organize our code in a more modular way, making it easier to manage dependencies and reduce complexity. Second, they can help us avoid naming conflicts and reduce namespace pollution, which can lead to more readable and maintainable code. And third, they can help us reduce compilation times and improve overall performance.

Imagine a world where you don’t have to wait 10 minutes for your code to compile just because you included a few too many headers. That’s the world that modules can help us create.

What’s Next for Modules?

C++ 20 modules are just the beginning. There are already plans to add new features and improvements to the module system in future versions of C++, including support for hierarchical modules, better tooling, and more. So if you’re a C++ developer, now is the time to start learning about modules and how to use them in your code.

So to sum up, C++ 20 modules are a much-needed addition to the language that can help us write cleaner, more maintainable, and more efficient code. So if you’re tired of dealing with header files and long compile times, give modules a try and see how they can improve your development experience.

Unknown's avatar

About Michael Haephrati מיכאל האפרתי

Michael Haephrati is a music composer, an inventor and an expert specializes in software development and information security, who has built a unique perspective which combines technology and the end user experience. Author of Learning C++ https://www.manning.com/books/learning-c-plus-plus
This entry was posted in Code Snippets and tagged , , , , , , , , . Bookmark the permalink.

Leave a comment