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
Copy file name to clipboardExpand all lines: docs/cpp/constexpr-cpp.md
+25-23Lines changed: 25 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,40 +1,42 @@
1
1
---
2
2
title: "constexpr (C++)"
3
-
ms.date: "08/05/2019"
3
+
description: "Guide to the C++ language constexpr keyword."
4
+
ms.date: "01/28/2020"
4
5
f1_keywords: ["constexpr_cpp"]
5
6
ms.assetid: c6458ccb-51c6-4a16-aa61-f69e6f4e04f7
7
+
no-loc: [constexpr, const, inline, goto, try, if, switch, for, while]
6
8
---
7
9
# constexpr (C++)
8
10
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.
10
12
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.
12
14
13
15
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).
One or more parameters, each of which must be a literal type and must itself be a constant expression.
26
28
27
-
## Return Value
29
+
## Return value
28
30
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).
30
32
31
33
## constexpr variables
32
34
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**.
34
36
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**.
36
38
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.
38
40
39
41
- All declarations of a **constexpr** variable or function must have the **constexpr** specifier.
40
42
@@ -49,7 +51,7 @@ constexpr int k = j + 1; //Error! j not a constant expression
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 timeto 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.)
53
55
54
56
A **constexpr** function or constructor is implicitly **inline**.
55
57
@@ -59,23 +61,23 @@ The following rules apply to constexpr functions:
59
61
60
62
- A **constexpr** function can be recursive.
61
63
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.
63
65
64
66
- The body can be defined as `= default` or `= delete`.
65
67
66
-
- The body can contain no **goto** statements or try blocks.
68
+
- The body can contain no **goto** statements or **try** blocks.
67
69
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**:
69
71
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**:
71
73
72
74
The following rules apply to **constexpr** functions in Visual Studio 2017 and later:
73
75
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**.
75
77
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.
77
79
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**.
79
81
80
82
```cpp
81
83
constexpr float exp(float x, int n)
@@ -91,11 +93,11 @@ constexpr float exp(float x, int n)
91
93
92
94
## extern constexpr
93
95
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**.
95
97
96
98
## Example
97
99
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.
99
101
100
102
```cpp
101
103
// constexpr.cpp
@@ -171,5 +173,5 @@ Visual Studio 2015 or later.
171
173
172
174
## See also
173
175
174
-
[Declarations and Definitions](../cpp/declarations-and-definitions-cpp.md)\
176
+
[Declarations and definitions](../cpp/declarations-and-definitions-cpp.md)\
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).
11
13
12
14
The **extern** keyword has four meanings depending on the context:
13
15
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).
18
23
19
24
## extern linkage for non-const globals
20
25
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.
22
27
23
28
```cpp
24
29
//fileA.cpp
@@ -37,7 +42,7 @@ extern int i = 43; // same error (extern is ignored on definitions)
37
42
38
43
## extern linkage for const globals
39
44
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:
41
46
42
47
```cpp
43
48
//fileA.cpp
@@ -49,23 +54,23 @@ extern const int i; // declaration only. same as i in FileA
49
54
50
55
## extern constexpr linkage
51
56
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**.
53
58
54
59
```cpp
55
60
externconstexprint x = 10; //error LNK2005: "int const x" already defined
56
61
```
57
62
58
-
If a header file contains a variable declared externconstexpr, 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:
59
64
60
65
```cpp
61
66
externconstexpr__declspec(selectany) int x = 10;
62
67
```
63
68
64
69
## extern "C" and extern "C++" function declarations
65
70
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.
67
72
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.
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:
110
115
111
116
```cpp
112
117
extern "C" int CFunc1();
@@ -123,8 +128,8 @@ extern "C" int CFunc2(); // Error: not the first declaration of
123
128
124
129
## See also
125
130
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)\
0 commit comments