You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enables experimental compiler support for C++ Standard modules. This option is obsolete for C++20 standard modules in Visual Studio version 16.11 and later. It's still required (along with [`/std:c++latest`](std-specify-language-standard-version.md)) for the experimental Standard library modules.
10
+
Enables compiler support for Microsoft's experimental form of C++ Standard modules. This option is obsolete in Visual Studio 2019 version 16.11 and later.
11
11
12
12
## Syntax
13
13
14
14
> **`/experimental:module`**\[**`-`**]
15
15
16
16
## Remarks
17
17
18
-
In versions of Visual Studio before Visual Studio 2019 version 16.11, you can enable experimental modules support by use of the **`/experimental:module`** compiler option along with the [`/std:c++latest`](std-specify-language-standard-version.md)option. In Visual Studio 2019 version 16.11, module support is enabled automatically by either **`/std:c++20`** or **`/std:c++latest`**. Use **`/experimental:module-`** to disable module support explicitly.
18
+
This switch was for the time before the new, standardized, way of consuming the C++ Standard Library as modules described in [Import the C++ standard library using modules](../../cpp/tutorial-import-stl-named-module.md)was available. Although you can use this switch to use the older experimental named modules, we recommend that you use the new, standardized, way of consuming the C++ Standard Library as modules described in [Import the C++ standard library using modules](../../cpp/tutorial-import-stl-named-module.md).
19
19
20
-
This option is available starting in Visual Studio 2015 Update 1. As of Visual Studio 2019 version 16.2, C++20 Standard modules aren't fully implemented in the Microsoft C++ compiler. Modules support is feature complete in Visual Studio 2019 version 16.10. You can use the modules feature import the Standard Library modules provided by Microsoft. A module and the code that consumes it must be compiled with the same compiler options.
20
+
This compiler became available starting in Visual Studio 2015 Update 1. Ensure that **C++ Modules for v143 build tools (x64/x86 - experimental)**in selected the VS Installer. It's available in the **Individual components** tab of the installer. Search for **experimental** to see the option. For more information, see [Install C and C++ support in Visual Studio](../vscpp-step-0-installation.md).
21
21
22
-
For more information on modules and how to use and create them, see [Overview of modules in C++](../../cpp/modules-cpp.md).
22
+
| Version | Status |
23
+
|---|---|
24
+
| Visual Studio 2015 Update 1 |`/experimental:module` introduced. |
25
+
| Visual Studio 2019 version 16.10 | C++20 modules support is feature complete. |
26
+
| Visual Studio 2019 16.11 and earlier | Enable experimental modules support using **`/experimental:module`** along with [`/std:c++latest`](std-specify-language-standard-version.md). |
27
+
| Visual Studio 2019 version 16.11 and later | Modules support is enabled automatically with **`/std:c++20`** or later, or **`/std:c++latest`**. Use **`/experimental:module-`** to disable experimental module support. |
28
+
29
+
The experimental library consists of the following named modules:
30
+
31
+
-`std.regex` provides the content of header `<regex>`
32
+
-`std.filesystem` provides the content of header `<filesystem>`
33
+
-`std.memory` provides the content of header `<memory>`
34
+
-`std.threading` provides the contents of headers `<atomic>`, `<condition_variable>`, `<future>`, `<mutex>`, `<shared_mutex>`, and `<thread>`
35
+
-`std.core` provides everything else in the C++ Standard Library
36
+
37
+
To consume these modules, add an import declaration to the top of the source code file. For example:
38
+
39
+
```cpp
40
+
import std.core;
41
+
import std.regex;
42
+
```
43
+
44
+
To consume the experimental Microsoft Standard Library modules, compile your program with the [`/EHsc`](eh-exception-handling-model.md) and [`/MD`](md-mt-ld-use-run-time-library.md) options.
23
45
24
46
### To set this compiler option in the Visual Studio development environment
25
47
26
48
1. Open the project's **Property Pages** dialog box. For details, see [Set C++ compiler and build properties in Visual Studio](../working-with-project-properties.md).
27
-
28
49
1. Set the **Configuration** drop-down to **All Configurations**.
description: Use import and export declarations to access and to publish types and functions defined in the specified module.
7
7
---
8
8
# `module`, `import`, `export`
9
9
10
-
The **`module`**, **`import`**, and **`export`** declarations are available in C++20 and require the [`/experimental:module`](../build/reference/experimental-module.md)compiler switch along with[`/std:c++20`](../build/reference/std-specify-language-standard-version.md) or later (such as **`/std:c++latest`**). For more information, see [Overview of modules in C++](modules-cpp.md).
10
+
The **`module`**, **`import`**, and **`export`** declarations are available in C++20 and require the compiler switch [`/std:c++20`](../build/reference/std-specify-language-standard-version.md) or later (such as **`/std:c++latest`**). For more information, see [Overview of modules in C++](modules-cpp.md).
11
11
12
12
## `module`
13
13
@@ -19,7 +19,7 @@ module ModuleA;
19
19
20
20
## `export`
21
21
22
-
Use an **`export module`** declaration for the module's primary interface file, which must have extension *`.ixx`*:
22
+
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](../build/reference/interface.md) switch to compile it as a module interface.
description: Modules in C++20 provide a modern alternative to header files.
6
6
---
@@ -14,44 +14,15 @@ You can use modules side by side with header files. A C++ source file can `impor
14
14
15
15
To contrast modules with other ways to import the standard library, see [Compare header units, modules, and precompiled headers](../build/compare-inclusion-methods.md).
16
16
17
-
## Enable modules in the Microsoft C++ compiler
17
+
Starting with Visual Studio 2022 version 17.5, importing the Standard Library as a module is both standardized and fully implemented in the Microsoft C++ compiler. To learn how to import the Standard Library using modules, see [Import the C++ standard library using modules](tutorial-import-stl-named-module.md).
18
18
19
-
As of Visual Studio 2022 version 17.1, C++20 standard modules are fully implemented in the Microsoft C++ compiler.
19
+
## Single-partition modules
20
20
21
-
Before it was specified by the C++20 standard, Microsoft had experimental support for modules. The compiler also supported importing prebuilt Standard Library modules, described below.
21
+
A single-partition module is a module that consists of a single source file. The module interface and implementation are in the same file.
22
22
23
-
Starting with Visual Studio 2022 version 17.5, importing the Standard Library as a module is both standardized and fully implemented in the Microsoft C++ compiler. This section describes the older, experimental method, which is still supported. For information about the new standardized way to import the Standard Library using modules, see [Import the C++ standard library using modules](tutorial-import-stl-named-module.md).
23
+
The following single-partition module example shows a simple module definition in a source file called *`Example.ixx`*. The *`.ixx`* extension is the default extension for module interface files in Visual Studio. If you want to use a different extension, use the [/interface](../build/reference/interface.md) switch to compile it as a module interface. In this example, the interface file contains both the function definition and the declaration. You can also place the definitions in one or more separate module implementation files, as shown in a later example, but this is an example of a single-partition module.
24
24
25
-
You can use the modules feature to create single-partition modules and to import the Standard Library modules provided by Microsoft. To enable support for Standard Library modules, compile with [`/experimental:module`](../build/reference/experimental-module.md) and [`/std:c++latest`](../build/reference/std-specify-language-standard-version.md). In a Visual Studio project, right-click the project node in **Solution Explorer** and choose **Properties**. Set the **Configuration** drop-down to **All Configurations**, then choose **Configuration Properties** > **C/C++** > **Language** > **Enable C++ Modules (experimental)**.
26
-
27
-
A module and the code that consumes it must be compiled with the same compiler options.
28
-
29
-
## Consume C++ Standard Library as modules (experimental)
30
-
31
-
This section describes the experimental implementation, which is still supported. The new standardized way of consuming the C++ Standard Library as modules is described in [Import the C++ standard library using modules](tutorial-import-stl-named-module.md).
32
-
33
-
By importing the C++ Standard Library as modules rather than including it through header files, you can potentially speed up compilation times depending on the size of your project. The experimental library is split into the following named modules:
34
-
35
-
-`std.regex` provides the content of header `<regex>`
36
-
-`std.filesystem` provides the content of header `<filesystem>`
37
-
-`std.memory` provides the content of header `<memory>`
38
-
-`std.threading` provides the contents of headers `<atomic>`, `<condition_variable>`, `<future>`, `<mutex>`, `<shared_mutex>`, and `<thread>`
39
-
-`std.core` provides everything else in the C++ Standard Library
40
-
41
-
To consume these modules, add an import declaration to the top of the source code file. For example:
42
-
43
-
```cpp
44
-
import std.core;
45
-
import std.regex;
46
-
```
47
-
48
-
To consume the Microsoft Standard Library modules, compile your program with the [`/EHsc`](../build/reference/eh-exception-handling-model.md) and [`/MD`](../build/reference/md-mt-ld-use-run-time-library.md) options.
49
-
50
-
## Example
51
-
52
-
The following example shows a simple module definition in a source file called *`Example.ixx`*. The *`.ixx`* extension is required for module interface files in Visual Studio. In this example, the interface file contains both the function definition and the declaration. However, you can also place the definitions in one or more separate module implementation files, as shown in a later example.
53
-
54
-
The `export module Example;` statement indicates that this file is the primary interface for a module called `Example`. The **`export`** modifier on `f()` indicates that this function is visible when another program or module imports `Example`.
25
+
The `export module Example;` statement indicates that this file is the primary interface for a module called `Example`. The **`export`** modifier before `int f()` indicates that this function is visible when another program or module imports `Example`:
55
26
56
27
```cpp
57
28
// Example.ixx
@@ -61,12 +32,14 @@ export module Example;
61
32
62
33
namespaceExample_NS
63
34
{
64
-
int f_internal() {
65
-
return ANSWER;
66
-
}
35
+
int f_internal()
36
+
{
37
+
return ANSWER;
38
+
}
67
39
68
-
export int f() {
69
-
return f_internal();
40
+
export int f()
41
+
{
42
+
return f_internal();
70
43
}
71
44
}
72
45
```
@@ -88,7 +61,7 @@ int main()
88
61
}
89
62
```
90
63
91
-
The `import` declaration can appear only at global scope.
64
+
The `import` declaration can appear only at global scope. A module and the code that consumes it must be compiled with the same compiler options.
92
65
93
66
## Module grammar
94
67
@@ -138,6 +111,7 @@ When the compiler does argument-dependent lookup for overload resolutions in the
138
111
### Module partitions
139
112
140
113
A module partition is similar to a module, except:
114
+
141
115
- It shares ownership of all declarations in the entire module.
142
116
- All names exported by partition interface files are imported and exported by the primary interface file.
143
117
- A partition's name must begin with the module name followed by a colon (`:`).
Copy file name to clipboardExpand all lines: docs/cpp/tutorial-import-stl-named-module.md
+3-5Lines changed: 3 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ This tutorial requires Visual Studio 2022 17.5 or later.
24
24
25
25
## Introduction to standard library modules
26
26
27
-
Header files suffer from semantics that can change depending on macro definitions, the order in which you include them, and they slow compilation. Modules solve these problems.
27
+
Header file semantics can change depending on macro definitions, the order in which you include them, and they slow compilation. Modules solve these problems.
28
28
29
29
It's now possible to import the standard library as a module instead of as a tangle of header files. This is much faster and more robust than including header files or header units or precompiled headers (PCH).
30
30
@@ -39,14 +39,12 @@ Because named modules don't expose macros, macros like `assert`, `errno`, `offse
39
39
40
40
## About C++ modules
41
41
42
-
Header files are how declarations and definitions have been shared between source files in C++. Prior to standard library modules, you'd include the part of the standard library you needed with a directive like `#include <vector>`. Header files are fragile and difficult to compose because their semantics may change depending on the order you include them, or whether certain macros are defined. They also slow compilation because they're reprocessed by every source file that includes them.
42
+
Header files are how declarations and definitions have been shared between source files in C++. Before standard library modules, you'd include the part of the standard library you needed with a directive like `#include <vector>`. Header files are fragile and difficult to compose because their semantics may change depending on the order you include them, or whether certain macros are defined. They also slow compilation because they're reprocessed by every source file that includes them.
43
43
44
44
C++20 introduces a modern alternative called *modules*. In C++23, we were able to capitalize on module support to introduce named modules to represent the standard library.
45
45
46
46
Like header files, modules allow you to share declarations and definitions across source files. But unlike header files, modules aren't fragile and are easier to compose because their semantics don't change due to macro definitions or the order in which you import them. The compiler can process modules much faster than it can process `#include` files, and uses less memory at compile time as well. Named modules don't expose macro definitions or private implementation details.
47
47
48
-
For in depth information about modules, see [Overview of modules in C++](modules-cpp.md) That article also discusses consuming the C++ standard library as modules, but uses an older and experimental way of doing it.
49
-
50
48
This article demonstrates the new and best way to consume the standard library. For more information about alternative ways to consume the standard library, see [Compare header units, modules, and precompiled headers](../build/compare-inclusion-methods.md).
51
49
52
50
## Import the standard library with `std`
@@ -57,7 +55,7 @@ The statement `import std;` or `import std.compat;` imports the standard library
57
55
58
56
### Example: How to build and import `std`
59
57
60
-
1. Open a x86 Native Tools Command Prompt for VS: from the Windows **Start** menu, type *x86 native* and the prompt should appear in the list of apps. Ensure that the prompt is for Visual Studio 2022 version 17.5 or above. You'll get errors if you use the wrong version of the prompt. The examples used in this tutorial are for the CMD shell.
58
+
1. Open a x86 Native Tools Command Prompt for VS: from the Windows **Start** menu, type *x86 native* and the prompt should appear in the list of apps. Ensure that the prompt is for Visual Studio 2022 version 17.5 or above. You get errors if you use the wrong version of the prompt. The examples used in this tutorial are for the CMD shell.
61
59
1. Create a directory, such as `%USERPROFILE%\source\repos\STLModules`, and make it the current directory. If you choose a directory that you don't have write access to, you'll get errors during compilation.
62
60
1. Compile the `std` named module with the following command:
0 commit comments