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
The **`const`** keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
23
50
@@ -30,7 +57,7 @@ int main() {
30
57
}
31
58
```
32
59
33
-
In C++, you can use the **`const`** keyword instead of the [#define](../preprocessor/hash-define-directive-c-cpp.md) preprocessor directive to define constant values. Values defined with **`const`** are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a **`const`** variable as follows:
60
+
In C++, you can use the **`const`** keyword instead of the [`#define`](../preprocessor/hash-define-directive-c-cpp.md) preprocessor directive to define constant values. Values defined with **`const`** are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a **`const`** variable as follows:
34
61
35
62
```cpp
36
63
// constant_values2.cpp
@@ -46,9 +73,10 @@ The **`const`** keyword can also be used in pointer declarations.
46
73
```cpp
47
74
// constant_values3.cpp
48
75
intmain() {
49
-
char *mybuf = 0, *yourbuf;
76
+
char this_char{'a'}, that_char{'b'};
77
+
char *mybuf = &this_char, *yourbuf = &that_char;
50
78
char *const aptr = mybuf;
51
-
*aptr = 'a'; // OK
79
+
*aptr = 'c'; // OK
52
80
aptr = yourbuf; // C3892
53
81
}
54
82
```
@@ -72,20 +100,20 @@ int main() {
72
100
73
101
You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer.
74
102
75
-
For objects that are declared as **`const`**, you can only call constant member functions. This ensures that the constant object is never modified.
103
+
For objects that are declared as **`const`**, you can only call constant member functions. The compiler ensures that the constant object is never modified.
76
104
77
105
```cpp
78
106
birthday.getMonth(); // Okay
79
107
birthday.setMonth( 4 ); // Error
80
108
```
81
109
82
-
You can call either constant or nonconstant member functions for a nonconstant object. You can also overload a member function using the **`const`** keyword; this allows a different version of the function to be called for constant and nonconstant objects.
110
+
You can call either constant or non-constant member functions for a non-constant object. You can also overload a member function using the **`const`** keyword; this feature allows a different version of the function to be called for constant and non-constant objects.
83
111
84
-
You cannot declare constructors or destructors with the **`const`** keyword.
112
+
You can't declare constructors or destructors with the **`const`** keyword.
85
113
86
-
## const member functions
114
+
## `const` member functions
87
115
88
-
Declaring a member function with the **`const`** keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.To declare a constant member function, place the **`const`** keyword after the closing parenthesis of the argument list. The **`const`** keyword is required in both the declaration and the definition.
116
+
Declaring a member function with the **`const`** keyword specifies that the function is a "read-only" function that doesn't modify the object for which it's called. A constant member function can't modify any non-static data members or call any member functions that aren't constant.To declare a constant member function, place the **`const`** keyword after the closing parenthesis of the argument list. The **`const`** keyword is required in both the declaration and the definition.
89
117
90
118
```cpp
91
119
// constant_member_function.cpp
@@ -117,7 +145,7 @@ int main()
117
145
}
118
146
```
119
147
120
-
## C and C++ const differences
148
+
## C and C++ `const` differences
121
149
122
150
When you declare a variable as **`const`** in a C source code file, you do so as:
123
151
@@ -147,17 +175,17 @@ to prevent name mangling by the C++ compiler.
147
175
148
176
## Remarks
149
177
150
-
When following a member function's parameter list, the **`const`** keyword specifies that the function does not modify the object for which it is invoked.
178
+
When following a member function's parameter list, the **`const`** keyword specifies that the function doesn't modify the object for which it's invoked.
151
179
152
-
For more information on **`const`**, see the following topics:
180
+
For more information on **`const`**, see the following articles:
153
181
154
-
- [const and volatile Pointers](../cpp/const-and-volatile-pointers.md)
182
+
- [`const` and `volatile` pointers](../cpp/const-and-volatile-pointers.md)
155
183
156
-
- [Type Qualifiers (C Language Reference)](../c-language/type-qualifiers.md)
184
+
- [Type qualifiers (C language reference)](../c-language/type-qualifiers.md)
description: Modules in C++20 provide a modern alternative to header files.
6
6
---
7
7
# Overview of modules in C++
8
8
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, so 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 can be reused by the compiler every place where the module is imported in a project.
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.
10
10
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, we suggest that you experiment with converting legacy headers to modules to see whether you get a meaningful reduction in compilation times.
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, we suggest that you experiment with converting legacy headers to modules. Base adoption on whether you get a meaningful reduction in compilation times.
12
12
13
13
## Enable modules in the Microsoft C++ compiler
14
14
@@ -37,7 +37,7 @@ To consume the Microsoft Standard Library module, compile your program with [`/E
37
37
38
38
## Basic example
39
39
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 the function definition as well as 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 will be visible when `Example` is imported by another program or module. Note that 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, 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 will be visible when `Example` is imported by another program or module. The module references a namespace `Example_NS`.
41
41
42
42
```cpp
43
43
exportmodule Example;
@@ -78,15 +78,15 @@ The `import` declaration can appear only at global scope.
78
78
79
79
## Implementing modules
80
80
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 kind of 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.
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.
82
82
83
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.
84
84
85
85
## Modules, namespaces, and argument-dependent lookup
86
86
87
87
The rules for namespaces in modules are the same as in any other code. If a declaration within a namespace is exported, the enclosing namespace (excluding non-exported members) is also implicitly exported. If a namespace is explicitly exported, all declarations within that namespace definition are exported.
88
88
89
-
When performing argument-dependent lookup for overload resolutions in the importing translation unit, the compiler considers functions declared in the same translation unit (including module interfaces) as where the type of the function's arguments are defined.
89
+
When it does argument-dependent lookup for overload resolutions in the importing translation unit, the compiler considers functions declared in the same translation unit (including module interfaces) as where the type of the function's arguments are defined.
90
90
91
91
### Module partitions
92
92
@@ -150,8 +150,8 @@ import std.filesystem;
150
150
Some headers are sufficiently self-contained that they may 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.
0 commit comments