Skip to content

Commit 3dd275e

Browse files
author
mikeblome
committed
intermediate checkin
1 parent f1a0a77 commit 3dd275e

1 file changed

Lines changed: 80 additions & 4 deletions

File tree

‎docs/cpp/modules-cpp.md‎

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,97 @@
11
---
22
title: "Overview of modules in C++"
3-
ms.date: "07/06/2019"
3+
ms.date: "07/15/2019"
44
helpviewer_keywords: ["modules [C++]", "modules [C++], overview"]
55
ms.description: Modules in C++20 represent a modern alternative to header files.
66
---
77

88
# Overview of modules in C++
99

10-
C++20 introduces *modules*, a modern solution for componentization of C++ libraries and programs. Modules eliminate or greatly reduce many of the problems associated with the use of header files, and also potentially reduce compilation times.
11-
When you *import* a module, you gain access to its public interface, which consists of types and functions that the module *exports*.
10+
C++20 introduces *modules*, a modern solution for componentization of C++ libraries and programs. Modules eliminate or greatly reduce many of the problems associated with the use of header files, and also potentially reduce compilation times. Macros and preprocessor directives declared in a module are not visible and therefore 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 do not participate in overload resolution or name lookup in the imported module. After a module is compiled, the result can be reused wherever else the module is imported.
1211

13-
## Obtaining modules
12+
Modules are intended as an eventual replacement for the use of the #include preprocessor directive and header files. However, 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. A module can import any number of other modules. Modules are orthogonal to namespaces. For example, multiple modules are used to expose the STL using the std namespace.
1413

1514
## Enabling modules in the Microsoft C++ compiler
1615

16+
To enable support for modules in Visual Studio 2017 and later, compile with `/experimental:modules`. 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)**.
17+
1718
## Importing modules
1819

20+
Use the **import** statement to make a module's exported names visible in your program.
21+
22+
The import declaration can appear only at the global scope. Any class completely defined along with all its members are made visible to the importing module. An incomplete class declaration in the export of a modu
23+
24+
Any names not exported are "owned" by the module and cannot conflict with names defined in your program or imported from other modules. A module cannot export macros
25+
26+
It is not possible for a module to export a macro, nor is it possible for a macro in an importing module to affect the imported module. Components that need to export macros should continue to use header files, with module-based subcomponents for the parts that are well behaved. For example, an existing library that provides interfaces controlled by a preprocessor macro symbol UNICODE can modularize its constituents and continue to provide a traditional header file-based solution as follows:
27+
28+
```cpp
29+
// Header file C.h
30+
#ifndef C_INCLUDED
31+
#define C_INCLUDED
32+
#ifdef UNICODE
33+
import C.Unicode;
34+
#else
35+
import C.Ansi;
36+
#endif // C INCLUDED
37+
```
38+
39+
Only exported names are visible outside the module; unlike a header file, a module owns the non-exported names that it declares. Non-exported names cannot be source of "One Definition Rule (ODR)" violations when a module is imported into a program or another module. A compiler error is raised if a program imports two modules that export identical names with the same type(s) in the same namespace.
40+
41+
The use of modules does not affect class member name lookup rules. A class that is declared (but not defined) in a module interface is seen as an incomplete type by any importing module, even if the module itself defines it in a non-exported name or function.
42+
43+
Modules are included in your program as source files, but unlike headers which must be parsed in every translation unit in which they appear, a module only needs to be compiled once. The compiler creates a binary interface file that describes all the exported types. That file can be processed much faster than a header file, and the binary interface file can be reused by the compiler every place where the module is imported. In larger projects, the reduction in compilation times can be significant.
44+
45+
### Legacy imports
46+
47+
Some headers are sufficiently self-contained that they are allowed to be brought in using the import keyword. An imported header is called a *legacy import*. Any #defines in the header itself are visible in the importing program.
48+
49+
```cpp
50+
import <vector>
51+
import "myheader.h"
52+
```
53+
54+
everything is exported including all #defines inside that file, but not any defines brought in from other included headers.
55+
56+
### Preamble
57+
58+
Import statements and #include statements only. No declarations. The #defines are immediately available even for other imported headers in the preamble.
59+
1960
## Creating modules
2061

62+
A module consists of one primary interface source file and zero or more implementation files or additional interface files. The primary interface file begins with the keywords `export module` followed by the name you give to the module. Any names that you intend to make visible outside the module, for example function or type names, must be preceded by the `export` keyword. You can define function bodies in the interface file itself, or in a separate implementation file.
63+
64+
The following example shows a minimal module definition:
65+
66+
```cpp
67+
export module Foo;
68+
69+
70+
```
71+
72+
impl units import module foo and can see the declarations. no export keyword allowed in an impl unit unless it's combined with the interface file. no worries about multiple definitions when imported by multiple files in the program.
73+
74+
a module unit is introduced by a declaration:
75+
module module-name ;
76+
This declaration means that subsequent declarations in the current translation unit are part of the module nominated by module-name. For simplicity, there can be at most one module declaration per translation unit. A module can span several module units — all of which must declare the module they belong to. Like most declarations in C++, it may be necessary to allow attributes on module declarations.
77+
78+
You can include header files in a module source file by putting the #include before the module declaration. These files are considered to be in the global module fragment, and no module "owns" them. module can only see symbols there that are in headers it specifically includes. The global module only contains symbols that are actually used.
79+
80+
### Module partitions
81+
82+
```cpp
83+
module baz:prt1
84+
...
85+
export module baz
86+
export import baz:part1
87+
```
88+
89+
you can import an implementation in a partition but not rexport it, allowing impl to remain hidden from end users
90+
91+
inline partitions?
92+
93+
### Argument dependent lookup when instantiating templates declared in a module
94+
95+
path of instantiation for templates declared in imported headers (30:03)
96+
2197
## See Also

0 commit comments

Comments
 (0)