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:
- The module
math
is defined in themath.ixx
file using theexport module
syntax. - The function
add
is marked withexport
, making it part of the module’s public interface. - In
main.cpp
, the module is imported usingimport math;
. - 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:
- The module
utilities
exports two functions:multiply
andsubtract
. - The module implementation is provided in
utilities.cpp
, where the functions are defined. - In
main.cpp
, the module is imported usingimport utilities;
. - The program uses the functions
multiply
andsubtract
from the module and prints the results.
Key Points about export
Keyword
- The
export
keyword is used in C++20 to define module interfaces, replacing traditional header files. - Exported declarations are part of the module’s public interface and can be accessed by other translation units.
- Modules improve compilation times, encapsulation, and reduce dependency-related issues.
- Pre-C++11 usage of
export
for templates is deprecated and largely unsupported by compilers. - To use modules, a C++20-compliant compiler is required with the
-std=c++20
flag enabled.