C++20 Modules

Header Units

What are header units, and how do they work in C++20?

Abstract art representing computer programming

Header units in C++20 are a bridge between traditional header files and modern modules. They allow you to import existing header files as if they were modules, facilitating the integration of legacy code with new module-based code.

Creating a Header Unit

To create a header unit, you use the import statement with the header file enclosed in quotes. This tells the compiler to treat the header file as a module-like entity. Here’s an example:

// Player.h
#pragma once
#include <iostream>
#include <string>

class Player {
 public:
  Player(const std::string &name) : name{name} {}
  void sayHello() const {
    std::cout << "Hello, my name is " << name;
  }

 private:
  std::string name;
};
// main.cpp
import "Player.h";

int main() {
  Player player{"John"};
  player.sayHello();
}
Hello, my name is John

Benefits of Header Units

  • Seamless Integration: Header units allow you to gradually transition from traditional headers to modules without rewriting all existing code.
  • Improved Compile Times: While not as efficient as pure modules, header units can still improve compile times by allowing the compiler to preprocess headers more efficiently.
  • Simplified Syntax: Using import instead of #include can simplify the dependency graph of your project, making it easier to manage and understand.

Limitations

  • Compiler Support: Not all compilers fully support header units yet, and the implementation details may vary between compilers.
  • Mixed Approaches: Using both header units and traditional #include directives can sometimes lead to inconsistencies and require careful management.

Example: Using Header Units with Legacy Code

Suppose you have a legacy project that heavily relies on traditional headers. You can start by converting some of these headers into header units:

// Utilities.h
#pragma once
#include <iostream>
#include <string>

void printMessage(const std::string &message) {
  std::cout << message << "\n";
}
// main.cpp
import "Utilities.h";

int main() {
  printMessage("Hello from header unit!");
}
Hello from header unit!

Summary

Header units in C++20 provide a way to import traditional header files as modules, enabling smoother integration of legacy code with new module-based projects.

They offer improved compile times and a cleaner syntax, although they come with some limitations in terms of compiler support and mixed usage.

Answers to questions are automatically generated and may not have been reviewed.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved