Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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++.
module
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;
export
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.ixx
export module ModuleA;
namespace ModuleA_NS
{
export int f();
export double d();
double internal_f(); // not exported
}
Nonexported names aren't visible to code that imports the module:
import ModuleA;
int main() {
ModuleA_NS::f(); // OK
ModuleA_NS::d(); // OK
ModuleA_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.
import
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 <class T>
class Baz
{...};
Remarks
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-name
export module ;
export module module-name
export 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.
Example
#define foo(...) __VA_ARGS__
foo(
import // Always an identifier, never a keyword
)
End Microsoft Specific
See also
Overview of modules in C++
Import the C++ standard library using modules