Skip to content

Commit 8744db4

Browse files
authored
Merge pull request #2648 from corob-msft/cr-1883
Fix 1883 bad link
2 parents 0cf0c88 + 567c846 commit 8744db4

2 files changed

Lines changed: 48 additions & 41 deletions

File tree

docs/cpp/constexpr-cpp.md

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,42 @@
11
---
22
title: "constexpr (C++)"
3-
ms.date: "08/05/2019"
3+
description: "Guide to the C++ language constexpr keyword."
4+
ms.date: "01/28/2020"
45
f1_keywords: ["constexpr_cpp"]
56
ms.assetid: c6458ccb-51c6-4a16-aa61-f69e6f4e04f7
7+
no-loc: [constexpr, const, inline, goto, try, if, switch, for, while]
68
---
79
# constexpr (C++)
810

9-
The keyword **constexpr** was introduced in C++11 and improved in C++14. It means *constant expression*. Like **const**, it can be applied to variables so that a compiler error is raised if any code attempts to modify the value. Unlike **const**, **constexpr** can also be applied to functions and class constructors. **constexpr** indicates that the value, or return value, is constant and, if possible, is computed at compile time.
11+
The keyword **constexpr** was introduced in C++11 and improved in C++14. It means *constant expression*. Like **const**, it can be applied to variables: A compiler error is raised when any code attempts to modify the value. Unlike **const**, **constexpr** can also be applied to functions and class constructors. **constexpr** indicates that the value, or return value, is constant and, where possible, is computed at compile time.
1012

11-
A **constexpr** integral value can be used wherever a const integer is required, such as in template arguments and array declarations. And when a value can be computed at compile time instead of run time, it can help your program run faster and use less memory.
13+
A **constexpr** integral value can be used wherever a const integer is required, such as in template arguments and array declarations. And when a value is computed at compile time instead of run time, it helps your program run faster and use less memory.
1214

1315
To limit the complexity of compile-time constant computations, and their potential impacts on compilation time, the C++14 standard requires the types in constant expressions to be [literal types](trivial-standard-layout-and-pod-types.md#literal_types).
1416

1517
## Syntax
1618

17-
> **constexpr** *literal-type* *identifier* **=** *constant-expression* **;**
18-
> **constexpr** *literal-type* *identifier* **{** *constant-expression* **}** **;**
19-
> **constexpr** *literal-type* *identifier* **(** *params* **)** **;**
19+
> **constexpr** *literal-type* *identifier* **=** *constant-expression* **;**\
20+
> **constexpr** *literal-type* *identifier* **{** *constant-expression* **}** **;**\
21+
> **constexpr** *literal-type* *identifier* **(** *params* **)** **;**\
2022
> **constexpr** *ctor* **(** *params* **)** **;**
2123
2224
## Parameters
2325

24-
*params*<br/>
26+
*params*\
2527
One or more parameters, each of which must be a literal type and must itself be a constant expression.
2628

27-
## Return Value
29+
## Return value
2830

29-
A constexpr variable or function must return a [literal type](trivial-standard-layout-and-pod-types.md#literal_types).
31+
A **constexpr** variable or function must return a [literal type](trivial-standard-layout-and-pod-types.md#literal_types).
3032

3133
## constexpr variables
3234

33-
The primary difference between const and constexpr variables is that the initialization of a const variable can be deferred until run time. A constexpr variable must be initialized at compile time. All constexpr variables are const.
35+
The primary difference between **const** and **constexpr** variables is that the initialization of a **const** variable can be deferred until run time. A **constexpr** variable must be initialized at compile time. All **constexpr** variables are **const**.
3436

35-
- A variable can be declared with **constexpr**, if it has a literal type and is initialized. If the initialization is performed by a constructor, the constructor must be declared as **constexpr**.
37+
- A variable can be declared with **constexpr**, when it has a literal type and is initialized. If the initialization is performed by a constructor, the constructor must be declared as **constexpr**.
3638

37-
- A reference may be declared as constexpr if the object that it references has been initialized by a constant expression and any implicit conversions that are invoked during initialization are also constant expressions.
39+
- A reference may be declared as **constexpr** when both these conditions are met: The referenced object is initialized by a constant expression, and any implicit conversions invoked during initialization are also constant expressions.
3840

3941
- All declarations of a **constexpr** variable or function must have the **constexpr** specifier.
4042

@@ -49,7 +51,7 @@ constexpr int k = j + 1; //Error! j not a constant expression
4951
5052
## <a name="constexpr_functions"></a> constexpr functions
5153
52-
A **constexpr** function is one whose return value can be computed at compile time when consuming code requires it. Consuming code requires the return value at compile time, for example, to initialize a **constexpr** variable or provide a non-type template argument. When its arguments are **constexpr** values, a **constexpr** function produces a compile-time constant. When called with non-**constexpr** arguments, or when its value isn't required at compile time, it produces a value at run time like a regular function. (This dual behavior saves you from having to write **constexpr** and non-**constexpr** versions of the same function.)
54+
A **constexpr** function is one whose return value is computable at compile time when consuming code requires it. Consuming code requires the return value at compile time to initialize a **constexpr** variable, or to provide a non-type template argument. When its arguments are **constexpr** values, a **constexpr** function produces a compile-time constant. When called with non-**constexpr** arguments, or when its value isn't required at compile time, it produces a value at run time like a regular function. (This dual behavior saves you from having to write **constexpr** and non-**constexpr** versions of the same function.)
5355
5456
A **constexpr** function or constructor is implicitly **inline**.
5557
@@ -59,23 +61,23 @@ The following rules apply to constexpr functions:
5961
6062
- A **constexpr** function can be recursive.
6163
62-
- It cannot be [virtual](../cpp/virtual-cpp.md). A constructor cannot be defined as constexpr if the enclosing class has any virtual base classes.
64+
- It can't be [virtual](../cpp/virtual-cpp.md). A constructor can't be defined as **constexpr** when the enclosing class has any virtual base classes.
6365
6466
- The body can be defined as `= default` or `= delete`.
6567
66-
- The body can contain no **goto** statements or try blocks.
68+
- The body can contain no **goto** statements or **try** blocks.
6769
68-
- An explicit specialization of a non-constexpr template can be declared as **constexpr**:
70+
- An explicit specialization of a non-**constexpr** template can be declared as **constexpr**:
6971
70-
- An explicit specialization of a **constexpr** template does not have to also be **constexpr**:
72+
- An explicit specialization of a **constexpr** template doesn't also have to be **constexpr**:
7173
7274
The following rules apply to **constexpr** functions in Visual Studio 2017 and later:
7375
74-
- It may contain **if** and **switch** statements, and all looping statements including **for**, range-based for, **while**, and **do-while**.
76+
- It may contain **if** and **switch** statements, and all looping statements including **for**, range-based **for**, **while**, and **do-while**.
7577
76-
- It may contain local variable declarations, but the variable must be initialized, must be a literal type, and cannot be static or thread-local. The locally declared variable isn't required to be const and may mutate.
78+
- It may contain local variable declarations, but the variable must be initialized. It must be a literal type, and can't be **static** or thread-local. The locally declared variable isn't required to be **const**, and may mutate.
7779
78-
- A constexpr non-static member function is not required to be implicitly const.
80+
- A **constexpr** non-**static** member function isn't required to be implicitly **const**.
7981
8082
```cpp
8183
constexpr float exp(float x, int n)
@@ -91,11 +93,11 @@ constexpr float exp(float x, int n)
9193
9294
## extern constexpr
9395

94-
The [/Zc:externConstexpr](../build/reference/zc-externconstexpr.md) compiler option causes the compiler to apply [external linkage](../c-language/external-linkage.md) to variables declared by using **extern constexpr**. In earlier versions of Visual Studio, and by default or if **/Zc:externConstexpr-** is specified, Visual Studio applies internal linkage to **constexpr** variables even if the **extern** keyword is used. The **/Zc:externConstexpr** option is available starting in Visual Studio 2017 Update 15.6, and is off by default. The /permissive- option does not enable **/Zc:externConstexpr**.
96+
The [/Zc:externConstexpr](../build/reference/zc-externconstexpr.md) compiler option causes the compiler to apply [external linkage](../c-language/external-linkage.md) to variables declared by using **extern constexpr**. In earlier versions of Visual Studio, either by default or when **/Zc:externConstexpr-** is specified, Visual Studio applies internal linkage to **constexpr** variables even when the **extern** keyword is used. The **/Zc:externConstexpr** option is available starting in Visual Studio 2017 Update 15.6, and is off by default. The [/permissive-](../build/reference/permissive-standards-conformance.md) option doesn't enable **/Zc:externConstexpr**.
9597

9698
## Example
9799

98-
The following example shows **constexpr** variables, functions, and a user-defined type. In the last statement in main(), the **constexpr** member function GetValue() is a run-time call because the value isn't required to be known at compile time.
100+
The following example shows **constexpr** variables, functions, and a user-defined type. In the last statement in `main()`, the **constexpr** member function `GetValue()` is a run-time call because the value isn't required to be known at compile time.
99101

100102
```cpp
101103
// constexpr.cpp
@@ -171,5 +173,5 @@ Visual Studio 2015 or later.
171173
172174
## See also
173175
174-
[Declarations and Definitions](../cpp/declarations-and-definitions-cpp.md)\
176+
[Declarations and definitions](../cpp/declarations-and-definitions-cpp.md)\
175177
[const](../cpp/const-cpp.md)

docs/cpp/extern-cpp.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
---
22
title: "extern (C++)"
3-
ms.date: "04/12/2018"
3+
description: "Guide to the C++ language extern keyword."
4+
ms.date: "01/28/2020"
45
f1_keywords: ["extern", "extern_CPP"]
56
helpviewer_keywords: ["extern keyword [C++], linkage to non-C++ functions", "declarations, external", "external linkage, extern modifier"]
67
ms.assetid: 1e2f0ae3-ae98-4410-85b5-222d6abc865a
8+
no-loc: [extern, const, constexpr, permissive]
79
---
810
# extern (C++)
911

10-
The **extern** keyword is applied to a global variable, function or template declaration to specify that the name of that thing has *external linkage*. For background information on linkage and why the use of global variables is discouraged, see [Translation units and linkage](program-and-linkage-cpp.md).
12+
The **extern** keyword may be applied to a global variable, function, or template declaration. It specifies that the symbol has *external linkage*. For background information on linkage and why the use of global variables is discouraged, see [Translation units and linkage](program-and-linkage-cpp.md).
1113

1214
The **extern** keyword has four meanings depending on the context:
1315

14-
1. in a non-const global variable declaration, **extern** specifies that the variable or function is defined in another translation unit. The **extern** must be applied in all files except the one where the variable is defined.
15-
1. in a const variable declaration, it specifies that the variable has external linkage. The **extern** must be applied to all declarations in all files. (Global const variables have internal linkage by default.)
16-
1. **extern "C"** specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.
17-
1. in a template declaration, it specifies that the template has already been instantiated elsewhere. This is an optimization that tells the compiler that it can re-use the other instantiation rather than creating a new one at the current location. For more information about this use of **extern**, see [Templates](templates-cpp.md).
16+
- In a non-**const** global variable declaration, **extern** specifies that the variable or function is defined in another translation unit. The **extern** must be applied in all files except the one where the variable is defined.
17+
18+
- In a **const** variable declaration, it specifies that the variable has external linkage. The **extern** must be applied to all declarations in all files. (Global **const** variables have internal linkage by default.)
19+
20+
- **extern "C"** specifies that the function is defined elsewhere and uses the C-language calling convention. The extern "C" modifier may also be applied to multiple function declarations in a block.
21+
22+
- In a template declaration, **extern** specifies that the template has already been instantiated elsewhere. **extern** tells the compiler it can reuse the other instantiation, rather than create a new one at the current location. For more information about this use of **extern**, see [Explicit instantiation](explicit-instantiation.md).
1823

1924
## extern linkage for non-const globals
2025

21-
When the linker sees **extern** before a global variable declaration, it looks for the definition in another translation unit. Declarations of non-const variables at global scope are external by default; only apply **extern** to the declarations that don't provide the definition.
26+
When the linker sees **extern** before a global variable declaration, it looks for the definition in another translation unit. Declarations of non-**const** variables at global scope are external by default. Only apply **extern** to the declarations that don't provide the definition.
2227

2328
```cpp
2429
//fileA.cpp
@@ -37,7 +42,7 @@ extern int i = 43; // same error (extern is ignored on definitions)
3742

3843
## extern linkage for const globals
3944

40-
A **const** global variable has internal linkage by default. If you want the variable to have external linkage, apply the **extern** keyword to definition as well as to all other declarations in other files:
45+
A **const** global variable has internal linkage by default. If you want the variable to have external linkage, apply the **extern** keyword to the definition, and to all other declarations in other files:
4146

4247
```cpp
4348
//fileA.cpp
@@ -49,23 +54,23 @@ extern const int i; // declaration only. same as i in FileA
4954

5055
## extern constexpr linkage
5156

52-
In Visual Studio 2017 version 15.3 and earlier, the compiler always gave a constexpr variable internal linkage even when the variable was marked extern. In Visual Studio 2017 version 15.5, a new compiler switch ([/Zc:externConstexpr](../build/reference/zc-externconstexpr.md)) enables correct standards-conforming behavior. Eventually this will become the default. The /permissive- option does not enable /Zc:externConstexpr.
57+
In Visual Studio 2017 version 15.3 and earlier, the compiler always gave a **constexpr** variable internal linkage, even when the variable was marked **extern**. In Visual Studio 2017 version 15.5 and later, the [/Zc:externConstexpr](../build/reference/zc-externconstexpr.md) compiler switch enables correct standards-conforming behavior. Eventually the option will become the default. The [/permissive-](../build/reference/permissive-standards-conformance.md) option doesn't enable **/Zc:externConstexpr**.
5358

5459
```cpp
5560
extern constexpr int x = 10; //error LNK2005: "int const x" already defined
5661
```
5762

58-
If a header file contains a variable declared extern constexpr, it needs to be marked **__declspec(selectany)** in order to correctly have its duplicate declarations combined:
63+
If a header file contains a variable declared **extern** **constexpr**, it must be marked `__declspec(selectany)` to correctly have its duplicate declarations combined:
5964

6065
```cpp
6166
extern constexpr __declspec(selectany) int x = 10;
6267
```
6368
6469
## extern "C" and extern "C++" function declarations
6570
66-
In C++, when used with a string, **extern** specifies that the linkage conventions of another language are being used for the declarator(s). C functions and data can be accessed only if they are previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.
71+
In C++, when used with a string, **extern** specifies that the linkage conventions of another language are being used for the declarator(s). C functions and data can be accessed only if they're previously declared as having C linkage. However, they must be defined in a separately compiled translation unit.
6772
68-
Microsoft C++ supports the strings **"C"** and **"C++"** in the *string-literal* field. All of the standard include files use the **extern** "C" syntax to allow the run-time library functions to be used in C++ programs.
73+
Microsoft C++ supports the strings **"C"** and **"C++"** in the *string-literal* field. All of the standard include files use the **extern "C"** syntax to allow the run-time library functions to be used in C++ programs.
6974
7075
## Example
7176
@@ -106,7 +111,7 @@ extern "C" char GetChar(void) {
106111
extern "C" int errno;
107112
```
108113

109-
If a function has more than one linkage specification, they must agree; it is an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program — one with a linkage specification and one without — the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration. For example:
114+
If a function has more than one linkage specification, they must agree. It's an error to declare functions as having both C and C++ linkage. Furthermore, if two declarations for a function occur in a program — one with a linkage specification and one without — the declaration with the linkage specification must be first. Any redundant declarations of functions that already have linkage specification are given the linkage specified in the first declaration. For example:
110115

111116
```cpp
112117
extern "C" int CFunc1();
@@ -123,8 +128,8 @@ extern "C" int CFunc2(); // Error: not the first declaration of
123128
124129
## See also
125130
126-
[Keywords](../cpp/keywords-cpp.md)<br/>
127-
[Translation units and linkage](program-and-linkage-cpp.md)<br/>
128-
[extern Storage-Class Specifier in C](../c-language/extern-storage-class-specifier.md)<br/>
129-
[Behavior of Identifiers in C](../c-language/behavior-of-identifiers.md)<br/>
130-
[Linkage in C](../c-language/linkage.md)
131+
[Keywords](../cpp/keywords-cpp.md)\
132+
[Translation units and linkage](program-and-linkage-cpp.md)\
133+
[extern Storage-Class Specifier in C](../c-language/extern-storage-class-specifier.md)\
134+
[Behavior of Identifiers in C](../c-language/behavior-of-identifiers.md)\
135+
[Linkage in C](../c-language/linkage.md)

0 commit comments

Comments
 (0)