16 How to be a super module: C++ Modules at work
This chapter covers
- Understanding namespaces and how to declare your own
- Diving deeper into the role of header files
- Exploring the general concept of C++ modules
- Understanding modules structure – interface units and implementation units
- Learning how to write and implement your own modules
Modules are a new feature in C++20 that provide a way to organize code into self-contained units, and are meant to replace traditional header files.
In this chapter, so you can fully understand the concept of modules, we start with some basics: first we explore namespaces, how are they used and how can we create our own namespaces.
Next, we provide a comprehensive overview of organizing C++ code using traditional header files. We explore the role of header files, and teach you how to structure header files effectively, including using include guards and organizing functions, and macros by functionality.
We then move and explore the role modules: You will understand what modules are, and how they are constructed from separate interface and implementation units. We will guide you on how to use the new export and import keywords, and the role each plays in implementing modules in your code. We will also explore some best practice and guidelines. By the end of this chapter, you will be equipped in how to use namespaces, header files and modules effectively, to write clean, efficient and maintainable code.