Skip to content

Commit f93bff4

Browse files
authored
Revise attributes documentation and update date
1 parent 158d9cb commit f93bff4

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

docs/cpp/attributes.md

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Attributes in C++"
33
description: "Learn more about: Attributes in C++"
44
f1_keywords: ["deprecated", "noreturn", "carries_dependency", "fallthrough", "nodiscard", "maybe_unused", "likely", "unlikely", "gsl::suppress", "msvc::flatten", "msvc::forceinline", "msvc::forceinline_calls", "msvc::intrinsic", "msvc::noinline", "msvc::noinline_calls", "msvc::no_tls_guard"]
55
helpviewer_keywords: ["deprecated", "noreturn", "carries_dependency", "fallthrough", "nodiscard", "maybe_unused", "likely", "unlikely", "gsl::suppress", "msvc::flatten", "msvc::forceinline", "msvc::forceinline_calls", "msvc::intrinsic", "msvc::noinline", "msvc::noinline_calls", "msvc::no_tls_guard"]
6-
ms.date: 4/13/2023
6+
ms.date: 12/8/2025
77
---
88

99
# Attributes in C++
@@ -158,39 +158,52 @@ void f() {
158158
i = my_move(i);
159159
}
160160
```
161+
161162
### `[[msvc::musttail]]`
162163

163-
The `[[msvc::musttail]]` attribute is an experimental x64 only Microsoft-specific attribute introduced in [MSVC Build Tools version 14.50](https://learn.microsoft.com/en-us/cpp/overview/what-s-new-for-msvc?view=msvc-170) that enforces tail-call optimization for a given function call. When applied on a qualifying return statement, it instructs the compiler to emit the call as a tail call. If the compiler cannot do so, it will produce a compilation error. The `[[msvc::musttail]]` attribute will enforce tail calling over inlining of a function.
164+
The `[[msvc::musttail]]` attribute, introduced in [MSVC Build Tools version 14.50](../overview/what-s-new-for-msvc.md#whats-new-for-msvc-build-tools-version-1450), is an experimental x64 only Microsoft-specific attribute that enforces tail-call optimization. When applied to a qualifying return statement, it instructs the compiler to emit the call as a tail call. If the compiler can't emit the tail call, it produces a compilation error. The `[[msvc::musttail]]` attribute enforces tail calling over function inlining.
164165

165-
The `[[msvc::musttail]]` attribute has constraints:
166+
`[[msvc::musttail]]` requirements:
166167
- The caller and callee must have matching return types.
167168
- The calling conventions must be compatible.
168169
- The tail call must be the final action in the calling function.
169-
- The callee must not use more stack space than the calling function.
170-
- If more than four integer parameters are passed, the calling function must allocate enough stack space for those additional arguments. Please see `__preserve_none` calling convention for tail calling without additional stack overhead.
171-
- Must compile with `/O2` or `/O2 /GL` optimization level
170+
- The callee can't use more stack space than the calling function.
171+
- If more than four integer parameters are passed, the calling function must allocate enough stack space for those additional arguments.
172+
- Compile with `/O2` or `/O2 /GL` optimization level.
172173

173174
#### Example
174175

175-
In the follwoing code, the `[[msvc::musttail]]` attribute applied to the `return bar(x)` call forces execution to jump from `foo` to `bar` before returning to `main`, rather than inlining `bar` into `foo` or calling `bar` from `foo` and returning to `foo` before returning to `main`.
176+
In the following code, the `[[msvc::musttail]]` attribute applied to `return increment(x)` causes execution to jump from `increment` to `incrementIfPositive` before returning to `main`, rather than inlining `increment` into `incrementIfPositive` or calling `increment` from `incrementIfPositive` and returning to `incrementIfPositive` before returning to `main`.
176177

177178
```cpp
178-
int bar(int x) {
179-
return x + 1;
180-
}
179+
// compile with /O2
180+
#include <iostream>
181181

182-
int foo(int x) {
183-
if(x > 0){
184-
[[msvc::musttail]]
185-
return bar(x);
182+
int increment(int x)
183+
{
184+
return x + 1;
186185
}
186+
187+
int incrementIfPositive(int x)
188+
{
189+
if (x > 0)
190+
{
191+
[[msvc::musttail]]
192+
return increment(x);
193+
}
187194
return -1;
188195
}
189196

190-
int main() {
191-
int result = foo(42);
192-
if(result < 0) return -1;
193-
return 0;
197+
int main()
198+
{
199+
int result = incrementIfPositive(42);
200+
if (result < 0)
201+
{
202+
return -1;
203+
}
204+
205+
std::cout << result; // outputs 43
206+
return 0;
194207
}
195208
```
196209

0 commit comments

Comments
 (0)