C++ export Keyword

The export keyword in C++ serves two distinct purposes depending on the C++ version being used:

  • In pre-C++11, it was used to allow the definition of template implementations in separate files (this usage is deprecated and largely unsupported).
  • In C++20, the export keyword is used for defining module interfaces, a modern feature aimed at improving compile times and encapsulation by replacing header files with modules.

Modules in C++20 are designed to provide a clean, efficient, and scalable way to manage large codebases by offering better isolation and reducing build times.


Syntax

The syntax for the export keyword in the context of modules is:

</>
Copy
export module ModuleName;

// Export declarations
export int add(int a, int b);
export
The keyword that marks a module or specific declarations as part of the module interface.
module ModuleName
Defines the module’s name.
export declarations
Functions, classes, or variables marked with export are part of the module’s public interface.

Examples

Example 1: Exporting a Simple Module

This example demonstrates how to define and use a simple module with an exported function.

File: math.ixx (Module Interface)

</>
Copy
export module math;

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

File: main.cpp

</>
Copy
import math; // Import the module

#include <iostream>
using namespace std;

int main() {
    cout << "Sum: " << add(5, 3) << endl;
    return 0;
}

Output:

Sum: 8

Explanation:

  1. The module math is defined in the math.ixx file using the export module syntax.
  2. The function add is marked with export, making it part of the module’s public interface.
  3. In main.cpp, the module is imported using import math;.
  4. The program calls the add function from the imported module and prints the result.

Example 2: Exporting Multiple Declarations

This example shows how to export multiple declarations in a module.

File: utilities.ixx (Module Interface)

</>
Copy
export module utilities;

export int multiply(int a, int b);
export int subtract(int a, int b);

File: utilities.cpp (Module Implementation)

</>
Copy
module utilities;

int multiply(int a, int b) {
    return a * b;
}

int subtract(int a, int b) {
    return a - b;
}

File: main.cpp

</>
Copy
import utilities; // Import the module

#include <iostream>
using namespace std;

int main() {
    cout << "Product: " << multiply(4, 5) << endl;
    cout << "Difference: " << subtract(10, 6) << endl;
    return 0;
}

Output:

Product: 20
Difference: 4

Explanation:

  1. The module utilities exports two functions: multiply and subtract.
  2. The module implementation is provided in utilities.cpp, where the functions are defined.
  3. In main.cpp, the module is imported using import utilities;.
  4. The program uses the functions multiply and subtract from the module and prints the results.

Key Points about export Keyword

  1. The export keyword is used in C++20 to define module interfaces, replacing traditional header files.
  2. Exported declarations are part of the module’s public interface and can be accessed by other translation units.
  3. Modules improve compilation times, encapsulation, and reduce dependency-related issues.
  4. Pre-C++11 usage of export for templates is deprecated and largely unsupported by compilers.
  5. To use modules, a C++20-compliant compiler is required with the -std=c++20 flag enabled.