title | ms.date | f1_keywords | helpviewer_keywords | description | ||||||
---|---|---|---|---|---|---|---|---|---|---|
module, import, export | 02/13/2025 |
|
| Use import and export declarations to access and to publish types and functions defined in the specified module. |
The module
, import
, and export
declarations are available in C++20 and require the compiler switch /std:c++20
or later. For more information, see Overview of modules in C++.
Place a module
declaration at the beginning of a module implementation file to specify that the file contents belong to the named module.
module ModuleA;
Use an export module
declaration for the module's primary interface file, which has an extension .ixx
by default. If you want to use a different extension, use the /interface switch to compile it as a module interface.
export module ModuleA;
In an interface file, use the export
modifier on names that are intended to be part of the public interface:
// ModuleA.ixxexport module ModuleA; namespaceModuleA_NS { exportintf(); exportdoubled(); doubleinternal_f(); // not exported }
Nonexported names aren't visible to code that imports the module:
import ModuleA; intmain() { ModuleA_NS::f(); // OKModuleA_NS::d(); // OKModuleA_NS::internal_f(); // Ill-formed: error C2065: 'internal_f': undeclared identifier }
The export
keyword may not appear in a module implementation file. When export
is applied to a namespace name, all names in the namespace are exported.
Use an import
declaration to make a module's names visible in your program. The import
declaration must appear after the module
declaration and after any #include
directives, but before any declarations in the file.
module ModuleA; #include"custom-lib.h" import std; import myModule; // begin declarations here:template <classT> classBaz {...};
Both import
and module
are treated as keywords only when they appear at the start of a logical line:
// OK: module ; module module-name import : import < import "import module-nameexport module ;export module module-nameexport import :export import <export import "export import module-name // Error:int i; module ;
Microsoft Specific
In Microsoft C++, the tokens import
and module
are always identifiers and never keywords when they're used as arguments to a macro.
#definefoo(...) __VA_ARGS__ foo( import // Always an identifier, never a keyword )
End Microsoft Specific
Overview of modules in C++
Import the C++ standard library using modules