Skip to content

Commit 60f8768

Browse files
author
Colin Robertson
authored
Merge pull request MicrosoftDocs#3681 from MicrosoftDocs/main637795099749737529
Repo sync for protected CLA branch
2 parents bf5462a + 2a31c72 commit 60f8768

File tree

4 files changed

+827
-801
lines changed

4 files changed

+827
-801
lines changed

docs/cpp/const-cpp.md

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
---
22
description: "Learn more about: const (C++)"
33
title: "const (C++)"
4-
ms.date: "11/04/2016"
4+
ms.date: 02/03/2022
55
f1_keywords: ["const_cpp"]
66
helpviewer_keywords: ["const keyword [C++]"]
77
ms.assetid: b21c0271-1ad0-40a0-b21c-5e812bba0318
88
---
9-
# const (C++)
9+
# `const` (C++)
1010

11-
When modifying a data declaration, the **`const`** keyword specifies that the object or variable is not modifiable.
11+
When it modifies a data declaration, the **`const`** keyword specifies that the object or variable isn't modifiable.
1212

1313
## Syntax
1414

15-
```
16-
const declaration ;
17-
member-function const ;
18-
```
19-
20-
## const values
15+
> *`declarator`*:\
16+
>  *`ptr-declarator`*\
17+
>  *`noptr-declarator`* *`parameters-and-qualifiers`* *`trailing-return-type`*\
18+
> *`ptr-declarator`*:\
19+
>  *`noptr-declarator`*\
20+
>  *`ptr-operator`* *`ptr-declarator`*\
21+
> *`noptr-declarator`*:\
22+
> &emsp;*`declarator-id`* *`attribute-specifier-seq`*<sub>opt</sub>\
23+
> &emsp;*`noptr-declarator`* *`parameters-and-qualifiers`*\
24+
> &emsp;*`noptr-declarator`* **`[`** *`constant-expression`*<sub>opt</sub> **`]`** *`attribute-specifier-seq`*<sub>opt</sub>\
25+
> &emsp;**`(`** *`ptr-declarator`* **`)`**\
26+
> *`parameters-and-qualifiers`*:\
27+
> &emsp;**`(`** *`parameter-declaration-clause`* **`)`** *`cv-qualifier-seq`*<sub>opt</sub>\
28+
> &emsp;*`ref-qualifier`*<sub>opt</sub> *`noexcept-specifier`*<sub>opt</sub> *`attribute-specifier-seq`*<sub>opt</sub>\
29+
> *`trailing-return-type`*:\
30+
> &emsp;**`->`** *`type-id`*\
31+
> *`ptr-operator`*:\
32+
> &emsp;**`*`** *`attribute-specifier-seq`*<sub>opt</sub> *`cv-qualifier-seq`*<sub>opt</sub>\
33+
> &emsp;**`&`** *`attribute-specifier-seq`*<sub>opt</sub>\
34+
> &emsp;**`&&`** *`attribute-specifier-seq`*<sub>opt</sub>\
35+
> &emsp;*`nested-name-specifier`* **`*`** *`attribute-specifier-seq`*<sub>opt</sub> *`cv-qualifier-seq`*<sub>opt</sub>\
36+
> *`cv-qualifier-seq`*:\
37+
> &emsp;*`cv-qualifier`* *`cv-qualifier-seq`*<sub>opt</sub>\
38+
> *`cv-qualifier`*:\
39+
> &emsp;**`const`**\
40+
> &emsp;**`volatile`**\
41+
> *`ref-qualifier`*:\
42+
> &emsp;**`&`**\
43+
> &emsp;**`&&`**\
44+
> *`declarator-id`*:\
45+
> &emsp;**`...`**<sub>opt</sub> *`id-expression`*
46+
47+
## `const` values
2148

2249
The **`const`** keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
2350

@@ -30,7 +57,7 @@ int main() {
3057
}
3158
```
3259

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:
3461

3562
```cpp
3663
// constant_values2.cpp
@@ -46,9 +73,10 @@ The **`const`** keyword can also be used in pointer declarations.
4673
```cpp
4774
// constant_values3.cpp
4875
int main() {
49-
char *mybuf = 0, *yourbuf;
76+
char this_char{'a'}, that_char{'b'};
77+
char *mybuf = &this_char, *yourbuf = &that_char;
5078
char *const aptr = mybuf;
51-
*aptr = 'a'; // OK
79+
*aptr = 'c'; // OK
5280
aptr = yourbuf; // C3892
5381
}
5482
```
@@ -72,20 +100,20 @@ int main() {
72100

73101
You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer.
74102

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.
76104

77105
```cpp
78106
birthday.getMonth(); // Okay
79107
birthday.setMonth( 4 ); // Error
80108
```
81109

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.
83111

84-
You cannot declare constructors or destructors with the **`const`** keyword.
112+
You can't declare constructors or destructors with the **`const`** keyword.
85113

86-
## const member functions
114+
## `const` member functions
87115

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.
89117

90118
```cpp
91119
// constant_member_function.cpp
@@ -117,7 +145,7 @@ int main()
117145
}
118146
```
119147
120-
## C and C++ const differences
148+
## C and C++ `const` differences
121149
122150
When you declare a variable as **`const`** in a C source code file, you do so as:
123151
@@ -147,17 +175,17 @@ to prevent name mangling by the C++ compiler.
147175
148176
## Remarks
149177
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.
151179
152-
For more information on **`const`**, see the following topics:
180+
For more information on **`const`**, see the following articles:
153181
154-
- [const and volatile Pointers](../cpp/const-and-volatile-pointers.md)
182+
- [`const` and `volatile` pointers](../cpp/const-and-volatile-pointers.md)
155183
156-
- [Type Qualifiers (C Language Reference)](../c-language/type-qualifiers.md)
184+
- [Type qualifiers (C language reference)](../c-language/type-qualifiers.md)
157185
158-
- [volatile](../cpp/volatile-cpp.md)
186+
- [`volatile`](../cpp/volatile-cpp.md)
159187
160-
- [#define](../preprocessor/hash-define-directive-c-cpp.md)
188+
- [`#define`](../preprocessor/hash-define-directive-c-cpp.md)
161189
162190
## See also
163191

docs/cpp/modules-cpp.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
22
title: "Overview of modules in C++"
3-
ms.date: 01/27/2022
3+
ms.date: 02/03/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, 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.
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, 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.
1212

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

@@ -37,7 +37,7 @@ To consume the Microsoft Standard Library module, compile your program with [`/E
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 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`.
4141

4242
```cpp
4343
export module Example;
@@ -78,15 +78,15 @@ The `import` declaration can appear only at global scope.
7878

7979
## Implementing modules
8080

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.
8282

8383
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.
8484

8585
## Modules, namespaces, and argument-dependent lookup
8686

8787
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.
8888

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.
9090

9191
### Module partitions
9292

@@ -150,8 +150,8 @@ import std.filesystem;
150150
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.
151151

152152
```cpp
153-
import <vector>
154-
import "myheader.h"
153+
import <vector>;
154+
import "myheader.h";
155155
```
156156

157157
## See also

0 commit comments

Comments
 (0)