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
Provides convenient asynchronous HTTP wrapper methods that are designed for cross-platform compatibility and for use in desktop apps on operating systems back to Windows 7 and Windows Server 2012. You can also use these in Universal Windows Platform apps; however, for apps that target only Universal Windows Platform, we recommend that you use the `Windows::Web:HttpClient` class. The C++ REST SDK (codename "Casablanca") also provides helper classes that support REST calls and convert JSON data into C++ types. The SDK is available on [GitHub](https://github.com/Microsoft/cpprestsdk).
A Windows Runtime HTTP client class modeled on the .NET Framework class of the same name in the System.Web namespace. `HttpClient` fully supports asynchronous upload and download over HTTP, and pipeline filters that enable the insertion of custom HTTP handlers into the pipeline. The Windows SDK includes sample filters for metered networks, OAuth authentication, and more.
63
+
A Windows Runtime HTTP client class modeled on the .NET Framework class of the same name in the System.Web namespace. `HttpClient` fully supports asynchronous upload and download over HTTP, and pipeline filters that enable the insertion of custom HTTP handlers into the pipeline. The Windows SDK includes sample filters for metered networks, OAuth authentication, and more. For apps that target only Universal Windows Platform, we recommend that you use the `Windows::Web:HttpClient` class.
Provides a native COM interface that you can use in Windows Store apps or Windows desktop apps to connect to the Internet over HTTP and issue GET, PUT, and other HTTP commands. For more information, see [Walkthrough: Connecting Using Tasks and XML HTTP Requests](../parallel/concrt/walkthrough-connecting-using-tasks-and-xml-http-requests.md).
Copy file name to clipboardExpand all lines: docs/cpp/constexpr-cpp.md
+49-23Lines changed: 49 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,12 +34,15 @@ translation.priority.ht:
34
34
- "zh-tw"
35
35
---
36
36
# constexpr (C++)
37
-
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 will be 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, will be computed at compile time. 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 can run faster and use less memory.
37
+
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 will be 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, will be computed at compile time. 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.
The primary difference between const and constexpr variables is that the initialization of a const variable can be deferred until run time whereas a constexpr variable must be initialized at compile time. All constexpr variables are const.
70
+
71
+
- 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`.
67
72
68
-
```
73
+
- 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.
74
+
75
+
- All declarations of a `constexpr` variable or function must have the `constexpr` specifier.
76
+
77
+
78
+
79
+
80
+
81
+
```cpp
69
82
constexpr float x = 42.0;
70
83
constexpr float y{108};
71
84
constexpr float z = exp(5, 3);
@@ -75,9 +88,38 @@ constexpr int k = j + 1; //Error! j not a constant expression
75
88
```
76
89
77
90
## constexpr functions
78
-
A `constexpr` function is one whose return value can be computed at compile when consuming code requires it. A `constexpr` function must accept and return only literal types. When its arguments are `constexpr` values, and consuming code requires the return value at compile time, for example to initialize a `constexpr` variable or provide a non-type template argument, it produces a compile-time constant. When called with non-`constexpr` arguments, or when its value is not 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.)
91
+
A `constexpr` function is one whose return value can be computed at compile when consuming code requires it. When its arguments are `constexpr` values, and consuming code requires the return value at compile time, for example to initialize a `constexpr` variable or provide a non-type template argument, it produces a compile-time constant. When called with non-`constexpr` arguments, or when its value is not 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.)
92
+
93
+
A `constexpr` function or constructor is implicitly `inline`.
94
+
95
+
The following rules apply to constexpr functions:
96
+
97
+
- A `constexpr` function must accept and return only literal types.
98
+
99
+
- A `constexpr` function can be recursive.
100
+
101
+
- It cannot be [virtual](../cpp/virtual-cpp.md). A a constructor cannot be defined as constexpr if the enclosing class has any virtual base classes.
102
+
103
+
- The body can be defined as `= default` or `= delete`.
104
+
105
+
- The body can contain no `goto` statements or try blocks.
106
+
107
+
- An explicit specialization of a non-constexpr template can be declared as `constexpr`:
108
+
109
+
- An explicit specialization of a `constexpr` template does not have to also be `constexpr`:
110
+
111
+
112
+
<!--conformance note-->
113
+
The following rules apply to constexpr functions in Visual Studio 2017 and later:
114
+
115
+
- It may contain if and switch statements, and all looping statements including for, range-based for, while, and do-while
116
+
117
+
- 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 is not required to be const and may mutate.
118
+
119
+
- A constexpr non-static member function is not required to be implicitly const.
120
+
79
121
80
-
```
122
+
```cpp
81
123
constexprfloatexp(float x, int n)
82
124
{
83
125
return n == 0 ? 1 :
@@ -89,23 +131,7 @@ constexpr float exp(float x, int n)
89
131
> [!TIP]
90
132
> Note: In the Visual Studio debugger, you can tell whether a `constexpr` function is being evaluated at compile time by putting a breakpoint inside it. If the breakpoint is hit, the function was called at run-time. If not, then the function was called at compile time.
91
133
92
-
## General constexpr rules
93
-
For a function, variable, constructor or static data member to be defined as `constexpr`, it must meet certain requirements:
94
-
95
-
- A `constexpr` function can be recursive. It cannot be [virtual](../cpp/virtual-cpp.md), and its return type and parameter types must all be literal types. The body can be defined as `= default` or `= delete`. Otherwise it must follow these rules: it contains no `goto` statements, try blocks, unitialized variables, or variable definitions that are not literal types, or that are static or thread-local. Additionally, a constructor cannot be defined as constexpr if the enclosing class has any virtual base classes.
96
-
97
-
- 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`.
98
-
99
-
- 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.
100
-
101
-
- All declarations of a `constexpr` variable or function must have the `constexpr` specifier.
102
-
103
-
- An explicit specialization of a non-constexpr template can be declared as `constexpr`:
104
-
105
-
- An explicit specialization of a `constexpr` template does not have to also be `constexpr`:
106
-
107
-
- A `constexpr` function or constructor is implicitly `inline`.
108
-
134
+
109
135
## Example
110
136
The following example shows `constexpr` variables, functions and a user-defined type. Note that in the last statement in main(), the `constexpr` member function GetValue() is a run-time call because the value is not required to be known at compile time.
111
137
@@ -182,4 +208,4 @@ int main()
182
208
183
209
## See Also
184
210
[Declarations and Definitions](../cpp/declarations-and-definitions-cpp.md)
Copy file name to clipboardExpand all lines: docs/cpp/initializers.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -388,7 +388,8 @@ int main() {
388
388
389
389
- no virtual member functions
390
390
391
-
- no brace-or-equal initializers for non-static members
391
+
> [!NOTE]
392
+
> <!--conformance note-->In Visual Studio 2015 and earlier, an aggregate is not allowed to have brace-or-equal initializers for non-static members. This restriction was removed in the C++14 standard and implemented in Visual Studio 2017.
392
393
393
394
Aggregate initializers consist of a braced initialization list, with or without an equals sign, as in the following example:
394
395
@@ -435,7 +436,7 @@ myArr3: 8 9 10 0 0
435
436
```
436
437
437
438
> [!IMPORTANT]
438
-
> Array members that declared but not explicitly initialized during aggregate initialization are zero-initialized, as in `myArr3` above.
439
+
> Array members that are declared but not explicitly initialized during aggregate initialization are zero-initialized, as in `myArr3` above.
439
440
440
441
#### Initializing unions and structs
441
442
If a union does not have a constructor, you can initialize it with a single value (or with another instance of a union). The value is used to initialize the first non-static field. This is different from struct initialization, in which the first value in the initializer is used to initialize the first field, the second to initialize the second field, and so on. Compare the initialization of unions and structs in the following example:
0 commit comments