Skip to content

Commit 205f874

Browse files
TaojunshenTylerMSFTJamesJBarnettlearn-build-service-prod[bot]huypub
authored
4/14/2023 AM Publish (#4879)
* alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text * Confirm merge from FromPublicMasterBranch to main to sync with https://github.com/MicrosoftDocs/cpp-docs (branch main) (#4867) * 3/31 AM Publish (#4859) * fix SAL annotation * acrolinx * go with _Out_opt_ and annotate the sample with why --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> * Update assert-asserte-assert-expr-macros.md (#4496) * 4/10/2023 AM Publish (#4868) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * Update attributes.md (#4498) * 4/11/2023 AM Publish (#4869) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * remove unneeded `To` * 4/12/2023 AM Publish (#4872) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> --------- Co-authored-by: huypub <[email protected]> Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com> Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: Learn Build Service GitHub App <Learn Build Service [email protected]> Co-authored-by: LuShuchen <[email protected]> Co-authored-by: Taojunshen <[email protected]> Co-authored-by: James Barnett <[email protected]> Co-authored-by: Terry Mahaffey <[email protected]> Co-authored-by: Igor Zhukov <[email protected]> Co-authored-by: Shannon Leavitt <[email protected]> * alphabetize the attributes * acrolinx * Correct typo * Bullet nonsequential list * Confirm merge from FromPublicMasterBranch to main to sync with https://github.com/MicrosoftDocs/cpp-docs (branch main) (#4878) * 3/31 AM Publish (#4859) * fix SAL annotation * acrolinx * go with _Out_opt_ and annotate the sample with why --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> * Update assert-asserte-assert-expr-macros.md (#4496) * 4/10/2023 AM Publish (#4868) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * Update attributes.md (#4498) * 4/11/2023 AM Publish (#4869) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * remove unneeded `To` * 4/12/2023 AM Publish (#4872) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * 4/13/2023 AM Publish (#4876) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text * Confirm merge from FromPublicMasterBranch to main to sync with https://github.com/MicrosoftDocs/cpp-docs (branch main) (#4867) * 3/31 AM Publish (#4859) * fix SAL annotation * acrolinx * go with _Out_opt_ and annotate the sample with why --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> * Update assert-asserte-assert-expr-macros.md (#4496) * 4/10/2023 AM Publish (#4868) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * Update attributes.md (#4498) * 4/11/2023 AM Publish (#4869) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> * remove unneeded `To` * 4/12/2023 AM Publish (#4872) * alt-text updates * acrolinx * fix image links, acrolinx * update alt-text * fixup text --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> --------- Co-authored-by: huypub <[email protected]> Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com> Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: Learn Build Service GitHub App <Learn Build Service [email protected]> Co-authored-by: LuShuchen <[email protected]> Co-authored-by: Taojunshen <[email protected]> Co-authored-by: James Barnett <[email protected]> Co-authored-by: Terry Mahaffey <[email protected]> Co-authored-by: Igor Zhukov <[email protected]> Co-authored-by: Shannon Leavitt <[email protected]> --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com> Co-authored-by: huypub <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: Learn Build Service GitHub App <Learn Build Service [email protected]> Co-authored-by: LuShuchen <[email protected]> Co-authored-by: Terry Mahaffey <[email protected]> Co-authored-by: Igor Zhukov <[email protected]> Co-authored-by: Shannon Leavitt <[email protected]> * Remove unmatched left bracket in stl-containers.md * Add missing quotation mark in algorithms.md * Fix punctuation typo in functional.md --------- Co-authored-by: huypub <[email protected]> Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com> Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: Learn Build Service GitHub App <Learn Build Service [email protected]> Co-authored-by: LuShuchen <[email protected]> Co-authored-by: Taojunshen <[email protected]> Co-authored-by: James Barnett <[email protected]> Co-authored-by: Terry Mahaffey <[email protected]> Co-authored-by: Igor Zhukov <[email protected]> Co-authored-by: Shannon Leavitt <[email protected]> Co-authored-by: Martin Schonger <[email protected]> Co-authored-by: Courtney Wales <[email protected]> --------- Co-authored-by: TylerMSFT <[email protected]> Co-authored-by: James Barnett <[email protected]> Co-authored-by: learn-build-service-prod[bot] <113403604+learn-build-service-prod[bot]@users.noreply.github.com> Co-authored-by: huypub <[email protected]> Co-authored-by: prmerger-automator[bot] <40007230+prmerger-automator[bot]@users.noreply.github.com> Co-authored-by: Learn Build Service GitHub App <Learn Build Service [email protected]> Co-authored-by: LuShuchen <[email protected]> Co-authored-by: Terry Mahaffey <[email protected]> Co-authored-by: Igor Zhukov <[email protected]> Co-authored-by: Shannon Leavitt <[email protected]> Co-authored-by: Dennis Rea <[email protected]> Co-authored-by: Martin Schonger <[email protected]> Co-authored-by: Courtney Wales <[email protected]>
1 parent 04ef4a1 commit 205f874

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

docs/cpp/attributes.md

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: "Learn more about: Attributes in C++"
33
title: "Attributes in C++"
44
f1_keywords: ["deprecated", "no_return", "carries_dependency", "fallthrough", "nodiscard", "maybe_unused", "likely", "unlikely", "gsl::suppress", "msvc::intrinsic", "msvc::no_tls_guard"]
55
helpviewer_keywords: ["deprecated", "no_return", "carries_dependency", "fallthrough", "nodiscard", "maybe_unused", "likely", "unlikely", "gsl::suppress", "msvc::intrinsic", "msvc::no_tls_guard"]
6-
ms.date: 12/06/2022
6+
ms.date: 4/13/2023
77
---
88
# Attributes in C++
99

@@ -31,22 +31,26 @@ void Foo(int);
3131
3232
Attributes represent a standardized alternative to vendor-specific extensions such as `#pragma` directives, `__declspec()` (Visual C++), or `__attribute__` (GNU). However, you'll still need to use the vendor-specific constructs for most purposes. The standard currently specifies the following attributes that a conforming compiler should recognize.
3333
34-
### `[[noreturn]]`
35-
36-
The `[[noreturn]]` attribute specifies that a function never returns; in other words, it always throws an exception or exits. The compiler can adjust its compilation rules for `[[noreturn]]` entities.
37-
3834
### `[[carries_dependency]]`
3935
4036
The `[[carries_dependency]]` attribute specifies that the function propagates data dependency ordering for thread synchronization. The attribute can be applied to one or more parameters, to specify that the passed-in argument carries a dependency into the function body. The attribute can be applied to the function itself, to specify that the return value carries a dependency out of the function. The compiler can use this information to generate more efficient code.
4137
4238
### `[[deprecated]]`
4339
44-
**Visual Studio 2015 and later:** The `[[deprecated]]` attribute specifies that a function isn't intended for use. Or, that it might not exist in future versions of a library interface. The `[[deprecated]]` attribute can be applied to declaration of a class, a typedef-name, a variable, a non-static data member, a function, a namespace, an enumeration, an enumerator, or a template specialization. The compiler can use this attribute to generate an informational message when client code attempts to call the function. When the Microsoft C++ compiler detects the use of a `[[deprecated]]` item, it raises compiler warning [C4996](../error-messages/compiler-warnings/compiler-warning-level-3-c4996.md).
40+
**Visual Studio 2015 and later:** The `[[deprecated]]` attribute specifies that a function isn't intended for use. Or, that it might not exist in future versions of a library interface. The `[[deprecated]]` attribute can be applied to declaration of a class, a typedef-name, a variable, a nonstatic data member, a function, a namespace, an enumeration, an enumerator, or a template specialization. The compiler can use this attribute to generate an informational message when client code attempts to call the function. When the Microsoft C++ compiler detects the use of a `[[deprecated]]` item, it raises compiler warning [C4996](../error-messages/compiler-warnings/compiler-warning-level-3-c4996.md).
4541
4642
### `[[fallthrough]]`
4743
4844
**Visual Studio 2017 and later:** (Available with [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) and later.) The `[[fallthrough]]` attribute can be used in the context of [`switch`](switch-statement-cpp.md) statements as a hint to the compiler (or anyone reading the code) that the fallthrough behavior is intended. The Microsoft C++ compiler currently doesn't warn on fallthrough behavior, so this attribute has no effect on compiler behavior.
4945
46+
### `[[likely]]`
47+
48+
**Visual Studio 2019 version 16.6 and later:** (Available with [`/std:c++20`](../build/reference/std-specify-language-standard-version.md) and later.) The `[[likely]]` attribute specifies a hint to the compiler that the code path for the attributed label or statement is more likely to execute than alternatives. In the Microsoft compiler, the `[[likely]]` attribute marks blocks as "hot code", which increments an internal optimization score. The score is incremented more when optimizing for speed, and not as much when optimizing for size. The net score affects the likelihood of inlining, loop unrolling, and vectorizing optimizations. The effect of `[[likely]]` and `[[unlikely]]` is similar to [Profile-guided optimization](../build/profile-guided-optimizations.md), but limited in scope to the current translation unit. The block reordering optimization isn't implemented yet for this attribute.
49+
50+
### `[[maybe_unused]]`
51+
52+
**Visual Studio 2017 version 15.3 and later:** (Available with [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) and later.) The `[[maybe_unused]]` attribute specifies that a variable, function, class, typedef, nonstatic data member, enum, or template specialization may be intentionally unused. The compiler doesn't warn when an entity marked `[[maybe_unused]]` isn't used. An entity that's declared without the attribute can later be redeclared with the attribute and vice-versa. An entity is considered *marked* after its first declaration that's marked `[[maybe_unused]]` gets analyzed, and for the rest of the current translation unit.
53+
5054
### `[[nodiscard]]`
5155
5256
**Visual Studio 2017 version 15.3 and later:** (Available with [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) and later.) Specifies that a function's return value isn't intended to be discarded. Raises warning [C4834](../error-messages/compiler-warnings/c4834.md), as shown in this example:
@@ -62,13 +66,9 @@ int main()
6266
}
6367
```
6468

65-
### `[[maybe_unused]]`
66-
67-
**Visual Studio 2017 version 15.3 and later:** (Available with [`/std:c++17`](../build/reference/std-specify-language-standard-version.md) and later.) The `[[maybe_unused]]` attribute specifies that a variable, function, class, typedef, non-static data member, enum, or template specialization may be intentionally unused. The compiler doesn't warn when an entity marked `[[maybe_unused]]` isn't used. An entity that's declared without the attribute can later be redeclared with the attribute and vice-versa. An entity is considered *marked* after its first declaration that's marked `[[maybe_unused]]` gets analyzed, and for the rest of the current translation unit.
68-
69-
### `[[likely]]`
69+
### `[[noreturn]]`
7070

71-
**Visual Studio 2019 version 16.6 and later:** (Available with [`/std:c++20`](../build/reference/std-specify-language-standard-version.md) and later.) The `[[likely]]` attribute specifies a hint to the compiler that the code path for the attributed label or statement is more likely to execute than alternatives. In the Microsoft compiler, the `[[likely]]` attribute marks blocks as "hot code", which increments an internal optimization score. The score is incremented more when optimizing for speed, and not as much when optimizing for size. The net score affects the likelihood of inlining, loop unrolling, and vectorizing optimizations. The effect of `[[likely]]` and `[[unlikely]]` is similar to [Profile-guided optimization](../build/profile-guided-optimizations.md), but limited in scope to the current translation unit. The block reordering optimization isn't implemented yet for this attribute.
71+
The `[[noreturn]]` attribute specifies that a function never returns; in other words, it always throws an exception or exits. The compiler can adjust its compilation rules for `[[noreturn]]` entities.
7272

7373
### `[[unlikely]]`
7474

@@ -103,15 +103,44 @@ The example raises these warnings:
103103

104104
The first two warnings fire when you compile this code with the CppCoreCheck code analysis tool installed and activated. But the third warning doesn't fire because of the attribute. You can suppress the entire bounds profile by writing `[[gsl::suppress(bounds)]]` without including a specific rule number. The C++ Core Guidelines are designed to help you write better and safer code. The suppress attribute makes it easy to turn off the warnings when they aren't wanted.
105105

106+
### `[[msvc::flatten]]`
107+
108+
The Microsoft-specific attribute `[[msvc::flatten]]` is very similar to `[[msvc::forceinline_calls]]`, and can be used in the same places and in the same way. The difference is that `[[msvc::flatten]]` will `[[msvc::forceinline_calls]]` all calls in the scope it's applied to recursively, until no calls are left. This may have consequences for the resulting code size growth of the function or the throughput of the compiler, which you must manage manually.
109+
110+
### `[[msvc::forceinline]]`
111+
112+
When placed before a function declaration, the Microsoft-specific attribute `[[msvc::forceinline]]` has the same meaning as `__forceinline`.
113+
114+
### `[[msvc::forceinline_calls]]`
115+
116+
The Microsoft-specific attribute `[[msvc::forceinline_calls]]` can be placed on or before a statement or a block. It causes the inline heuristic to attempt to `[[msvc::forceinline]]` all calls in that statement or block:
117+
118+
```cpp
119+
void f() {
120+
[[msvc::forceinline_calls]]
121+
{
122+
foo();
123+
bar();
124+
}
125+
...
126+
[[msvc::forceinline_calls]]
127+
bar();
128+
129+
foo();
130+
}
131+
```
132+
133+
The first call to `foo`, and both calls to `bar`, are treated as if they were declared `__forceinline`. The second call to `foo` isn't treated as `__forceinline`.
134+
106135
### `[[msvc::intrinsic]]`
107136

108137
The Microsoft-specific `[[msvc::intrinsic]]` attribute tells the compiler to inline a metafunction that acts as a named cast from the parameter type to the return type. When the attribute is present on a function definition, the compiler replaces all calls to that function with a simple cast. The `[[msvc::intrinsic]]` attribute is available in Visual Studio 2022 version 17.5 preview 2 and later versions. This attribute applies only to the specific function that follows it.
109138

110-
The `[[msvc::intrinsic]]` attribute has two constraints on the function it's applied to:
139+
The `[[msvc::intrinsic]]` attribute has three constraints on the function it's applied to:
111140

112-
1. The function can't be recursive; its body must only have a return statement with a cast.
113-
1. The function can only accept a single parameter.
114-
1. The **`/permissive-`** compiler option is required. (The **`/std:c++20`** and later options imply **`/permissive-`** by default.)
141+
- The function can't be recursive; its body must only have a return statement with a cast.
142+
- The function can only accept a single parameter.
143+
- The **`/permissive-`** compiler option is required. (The **`/std:c++20`** and later options imply **`/permissive-`** by default.)
115144

116145
#### Example
117146

@@ -127,6 +156,14 @@ void f() {
127156
}
128157
```
129158

159+
### `[[msvc::noinline]]`
160+
161+
When placed before a function declaration, the Microsoft-specific attribute `[[msvc::noinline]]` has the same meaning as `declspec(noinline)`.
162+
163+
### `[[msvc::noinline_calls]]`
164+
165+
The Microsoft-specific attribute `[[msvc::noinline_calls]]` has the same usage as `[[msvc::forceinline_calls]]`. It can be placed before any statement or block. Rather than force-inlining all calls in that block, it has the effect of turning off inlining for the scope it's applied to.
166+
130167
### `[[msvc::no_tls_guard]]`
131168

132169
The Microsoft-specific `[[msvc::no_tls_guard]]` attribute disables checks for initialization on first access to thread-local variables in DLLs. The checks are enabled by default in code built using Visual Studio 2019 version 16.5 and later versions. This attribute applies only to the specific variable that follows it. To disable checks globally, use the [`/Zc:tlsGuards-`](../build/reference/zc-tlsguards.md) compiler option.

docs/standard-library/algorithms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The descriptions of the algorithm function templates employ several shorthand ph
1818

1919
- The phrase "the lowest value of *N* in the range \[*A*, *B*) such that *X*" means that the condition *X* is determined for each *N* in the range \[*A*, *B*) until the condition *X* is met.
2020

21-
- The phrase "the highest value of *N* in the range \[*A*, *B*) such that *X* means that *X* is determined for each *N* in the range \[*A*, *B*). The function stores in *K* a copy of *N* each time the condition *X* is met. If any such store occurs, the function replaces the final value of *N*, which equals *B*, with the value of *K*. For a bidirectional or random-access iterator, however, it can also mean that *N* begins with the highest value in the range and is decremented over the range until the condition *X* is met.
21+
- The phrase "the highest value of *N* in the range \[*A*, *B*) such that *X*" means that *X* is determined for each *N* in the range \[*A*, *B*). The function stores in *K* a copy of *N* each time the condition *X* is met. If any such store occurs, the function replaces the final value of *N*, which equals *B*, with the value of *K*. For a bidirectional or random-access iterator, however, it can also mean that *N* begins with the highest value in the range and is decremented over the range until the condition *X* is met.
2222

2323
- Expressions such as *X* - *Y*, where *X* and *Y* can be iterators other than random-access iterators, are intended in the mathematical sense. The function doesn't necessarily evaluate operator **-** if it must determine such a value. The same is also true for expressions such as *X* + *N* and *X* - *N*, where *N* is an integer type.
2424

docs/standard-library/functional.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Defines C++ Standard Library functions that help construct *function objects*, a
2020

2121
Algorithms require two types of function objects: *unary* and *binary*. Unary function objects require one argument, and binary function objects require two arguments. A function object and function pointers can be passed as a predicate to an algorithm, but function objects are also adaptable and increase the scope, flexibility, and efficiency of the C++ Standard Library. If, for example, a value needed to be bound to a function before being passed to an algorithm, then a function pointer could not be used. Function adaptors convert function pointers into adaptable function objects that can be bound to a value. The header \<functional> also contains member function adaptors that allow member functions to be called as adaptable function objects. Functions are adaptable if they have nested type declarations specifying their argument and return types. Function objects and their adaptors allow the C++ Standard Library to upgrade existing applications and help integrate the library into the C++ programming environment.
2222

23-
The implementation of the function objects in \<functional> includes *transparent operator functors*. which are specializations of standard function objects and take no template parameters, and perform perfect forwarding of the function arguments and perfect return of the result. These template specializations do not require that you specify argument types when you invoke arithmetic, comparison, logical, and bitwise operator functors. You can overload arithmetic, comparison, logical, or bitwise operators for your own types, or for heterogeneous combinations of types, and then use the transparent operator functors as function arguments. For example, if your type *MyType* implements `operator<`, you can call `sort(my_collection.begin(), my_collection.end(), less<>())` instead of explicitly specifying the type `sort(my_collection.begin(), my_collection.end(), less<MyType>())`.
23+
The implementation of the function objects in \<functional> includes *transparent operator functors*, which are specializations of standard function objects and take no template parameters, and perform perfect forwarding of the function arguments and perfect return of the result. These template specializations do not require that you specify argument types when you invoke arithmetic, comparison, logical, and bitwise operator functors. You can overload arithmetic, comparison, logical, or bitwise operators for your own types, or for heterogeneous combinations of types, and then use the transparent operator functors as function arguments. For example, if your type *MyType* implements `operator<`, you can call `sort(my_collection.begin(), my_collection.end(), less<>())` instead of explicitly specifying the type `sort(my_collection.begin(), my_collection.end(), less<MyType>())`.
2424

2525
The following features are added in C++11, C++14 and C++17:
2626

docs/standard-library/stl-containers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ In general, elements inserted into a C++ Standard Library container can be of ju
142142

143143
The destructor isn't permitted to throw an exception.
144144

145-
Ordered associative containers—described earlier in this article—must have a public comparison operator defined. (By default, the operator is `operator<`, but even types that don't work with `operator<` are supported.
145+
Ordered associative containers—described earlier in this article—must have a public comparison operator defined. By default, the operator is `operator<`, but even types that don't work with `operator<` are supported.
146146

147147
Some operations on containers might also require a public default constructor and a public equivalence operator. For example, the unordered associative containers require support for equality and hashing.
148148

0 commit comments

Comments
 (0)