Skip to content

Commit 1f11bde

Browse files
Componentize is not a good word.
1 parent 3fd1d6e commit 1f11bde

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

docs/cpp/modules-cpp.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
title: "Overview of modules in C++"
3-
ms.date: 02/14/2022
3+
ms.date: 03/30/2022
44
helpviewer_keywords: ["modules [C++]", "modules [C++], overview"]
55
description: Modules in C++20 provide a modern alternative to header files.
66
---
77
# Overview of modules in C++
88

9-
C++20 introduces *modules*, a modern solution for componentization of C++ libraries and programs. A module is a set of source code files that are compiled independently of the [translation units](https://wikipedia.org/wiki/Translation_unit_(programming)) that import them. Modules eliminate or reduce many of the problems associated with the use of header files. They often reduce compilation times. Macros, preprocessor directives, and non-exported names declared in a module aren't visible outside the module. They have no effect on the compilation of the translation unit that imports the module. You can import modules in any order without concern for macro redefinitions. Declarations in the importing translation unit don't participate in overload resolution or name lookup in the imported module. After a module is compiled once, the results are stored in a binary file that describes all the exported types, functions, and templates. That file can be processed much faster than a header file. And, the compiler can reuse it every place where the module is imported in a project.
9+
C++20 introduces *modules*, a modern solution that turns C++ libraries and programs into components. A *module* is a set of source code files that are compiled independently of the [translation units](https://wikipedia.org/wiki/Translation_unit_(programming)) that import them. Modules eliminate or reduce many of the problems associated with the use of header files. They often reduce compilation times. Macros, preprocessor directives, and non-exported names declared in a module aren't visible outside the module. They have no effect on the compilation of the translation unit that imports the module. You can import modules in any order without concern for macro redefinitions. Declarations in the importing translation unit don't participate in overload resolution or name lookup in the imported module. After a module is compiled once, the results are stored in a binary file that describes all the exported types, functions, and templates. The compiler can process that file much faster than a header file. And, the compiler can reuse it every place where the module is imported in a project.
1010

11-
Modules can be used side by side with header files. A C++ source file can import modules and also #include header files. In some cases, a header file can be imported as a module rather than textually #included by the preprocessor. We recommend you use modules in new projects rather than header files as much as possible. For larger existing projects under active development, experiment with converting legacy headers to modules. Base adoption on whether you get a meaningful reduction in compilation times.
11+
You can use modules side by side with header files. A C++ source file can `import` modules and also `#include` header files. In some cases, you can import a header file as a module rather than include it textually by using `#include` in the preprocessor. We recommend you use modules in new projects rather than header files as much as possible. For larger existing projects under active development, experiment with converting legacy headers to modules. Base your adoption on whether you get a meaningful reduction in compilation times.
1212

1313
## Enable modules in the Microsoft C++ compiler
1414

@@ -18,7 +18,7 @@ A module and the code that consumes it must be compiled with the same compiler o
1818

1919
## Consume the C++ Standard Library as modules
2020

21-
Although not specified by the C++20 standard, Microsoft enables its implementation of the C++ Standard Library to be imported as modules. 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 library is componentized into the following modules:
21+
Although not specified by the C++20 standard, Microsoft makes its implementation of the C++ Standard Library importable as modules. 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 library is split into the following named modules:
2222

2323
- `std.regex` provides the content of header `<regex>`
2424
- `std.filesystem` provides the content of header `<filesystem>`
@@ -33,13 +33,14 @@ import std.core;
3333
import std.regex;
3434
```
3535

36-
To consume the Microsoft Standard Library module, compile your program with [`/EHsc`](../build/reference/eh-exception-handling-model.md) and [`/MD`](../build/reference/md-mt-ld-use-run-time-library.md) options.
36+
To consume the Microsoft Standard Library modules, compile your program with [`/EHsc`](../build/reference/eh-exception-handling-model.md) and [`/MD`](../build/reference/md-mt-ld-use-run-time-library.md) options.
3737

3838
## Basic example
3939

40-
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, the definitions can be also placed in one or more separate files (as shown in a later example). 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 `Example` is imported by another program or module. The module references a namespace `Example_NS`.
40+
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. 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 `Example` is imported by another program or module. The module references a namespace `Example_NS`.
4141

4242
```cpp
43+
// Example.ixx
4344
export module Example;
4445

4546
#define ANSWER 42
@@ -59,7 +60,7 @@ namespace Example_NS
5960
The file *`MyProgram.cpp`* uses the **`import`** declaration to access the name that is exported by `Example`. The name `Example_NS` is visible here, but not all of its members. Also, the macro `ANSWER` isn't visible.
6061

6162
```cpp
62-
63+
// MyProgram.cpp
6364
import Example;
6465
import std.core;
6566

@@ -71,16 +72,50 @@ int main()
7172
// int i = Example_NS::f_internal(); // C2039
7273
// int j = ANSWER; //C2065
7374
}
74-
7575
```
7676

7777
The `import` declaration can appear only at global scope.
7878

79+
## Module grammar
80+
81+
> *`module-name`*:\
82+
> &emsp;*`module-name-qualifier-seq`*<sub>opt</sub> *`identifier`*
83+
>
84+
> *`module-name-qualifier-seq`*:\
85+
> &emsp;*`identifier`* **`.`**\
86+
> &emsp;*`module-name-qualifier-seq`* *`identifier`* **`.`**
87+
>
88+
> *`module-partition`*:\
89+
> &emsp;**`:`** *`module-name`*
90+
>
91+
> *`module-declaration`*:\
92+
> &emsp;**`export`**<sub>opt</sub> **`module`** *`module-name`* *`module-partition`*<sub>opt</sub> *`attribute-specifier-seq`*<sub>opt</sub> **`;`**
93+
>
94+
> *`module-import-declaration`*:\
95+
> &emsp;**`export`**<sub>opt</sub> **`import`** *`module-name`* *`attribute-specifier-seq`*<sub>opt</sub> **`;`**\
96+
> &emsp;**`export`**<sub>opt</sub> **`import`** *`module-partition`* *`attribute-specifier-seq`*<sub>opt</sub> **`;`**\
97+
> &emsp;**`export`**<sub>opt</sub> **`import`** *`header-name`* *`attribute-specifier-seq`*<sub>opt</sub> **`;`**
98+
>
99+
79100
## Implementing modules
80101

81-
You can create a module with a single interface file (*`.ixx`*) that exports names and includes implementations of all functions and types. You can also put the implementations in one or more separate implementation files, similar to how .h and .cpp files are used. The **`export`** keyword is used in the interface file only. An implementation file can **`import`** another module, but can't **`export`** any names. Implementation files may be named with any extension. An interface file and the backing set of implementation files are treated as a special variation of a translation unit called a *module unit*. A name that's declared in any implementation file is automatically visible in all other files within the same module unit.
102+
A *module interface* exports the module name and all the namespaces, types, functions and so on that make up the public interface of the module. A *module implementation* defines the things exported by the module. In its simplest form, a module can consist of a single file that combines the module interface and implementation. You can also put the implementations in one or more separate module implementation files, similar to how *`.h`* and *`.cpp`* files are used.
103+
104+
For larger modules, you can split parts of the module into submodules called *partitions*. Each partition consists of a module interface file that exports a module partition name. A partition may also have one or more partition implementation files. The module as a whole has one *primary module interface*, the public interface of the module that may also import and export the partition interfaces.
105+
106+
A module consists of one or more *module units*. A module unit is a translation unit (a source file) that contains a module declaration. There are several types of module units:
107+
108+
- A *module interface unit* is a module unit that exports a module name or module partition name. A module interface unit has `export module` in its module declaration.
109+
110+
- A *module implementation unit* is a module unit that doesn't export a module name or module partition name. As the name implies, it's used to implement a module.
111+
112+
- A *primary module interface unit* is a module interface unit that exports the module name. There must be one and only one primary module interface unit in a module.
113+
114+
- A *module partition interface unit* is a module interface unit that exports a module partition name.
115+
116+
- A *module partition implementation unit* is a module implementation unit that has a module partition name in its module declaration, but no `export` keyword.
82117

83-
For larger modules, you can split the module into multiple module units called *partitions*. Each partition consists of an interface file backed by one or more implementation files.
118+
The **`export`** keyword is used in interface files only. An implementation file can **`import`** another module, but can't **`export`** any names. Implementation files can have any extension.
84119

85120
## Modules, namespaces, and argument-dependent lookup
86121

@@ -90,7 +125,7 @@ When it does argument-dependent lookup for overload resolutions in the importing
90125

91126
### Module partitions
92127

93-
A module can be componentized into *partitions*, each consisting of an interface file and zero or more implementation files. A module partition is similar to a module, except it shares ownership of all declarations in the entire module. All names exported by partition interface files are imported and re-exported by the primary interface file. A partition's name must begin with the module name followed by a colon. Declarations in any of the partitions are visible within the entire module. No special precautions are needed to avoid one-definition-rule (ODR) errors. You can declare a name (function, class, and so on) in one partition and define it in another. A partition implementation file begins like this:
128+
A module partition is similar to a module, except it shares ownership of all declarations in the entire module. All names exported by partition interface files are imported and re-exported by the primary interface file. A partition's name must begin with the module name followed by a colon. Declarations in any of the partitions are visible within the entire module. No special precautions are needed to avoid one-definition-rule (ODR) errors. You can declare a name (function, class, and so on) in one partition and define it in another. A partition implementation file begins like this:
94129

95130
```cpp
96131
module Example:part1;
@@ -147,7 +182,7 @@ import std.filesystem;
147182

148183
### Imported header files
149184

150-
Some headers are sufficiently self-contained that they can be brought in using the **`import`** keyword. The main difference between an imported header and an imported module is that any preprocessor definitions in the header are visible in the importing program immediately after the `import` statement. However, preprocessor definitions in any files included by that header aren't visible.
185+
Some headers are sufficiently self-contained that they can be brought in using the **`import`** keyword. The main difference between an imported header and an imported module is that any preprocessor definitions in the header are visible in the importing program immediately after the `import` statement.
151186

152187
```cpp
153188
import <vector>;
@@ -156,4 +191,5 @@ import "myheader.h";
156191

157192
## See also
158193

159-
[`module`, `import`, `export`](import-export-module.md)
194+
[`module`, `import`, `export`](import-export-module.md)\
195+
[Named modules tutorial](tutorial-named-modules-cpp.md)

docs/ide/walkthrough-testing-a-project-cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ In this walkthrough, you watch the value of a variable as the program runs and d
1919

2020
### To run a program in Debug mode
2121

22-
1. Open Games.cpp for editing.
22+
1. Open Game.cpp for editing.
2323

2424
1. Select this line of code:
2525

0 commit comments

Comments
 (0)