Skip to content

Commit 6414c06

Browse files
authored
Update noreturn.md
Use cpub coding style. Update the name to be a little more descriptive of what it does. Minor edits.
1 parent a409df5 commit 6414c06

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

docs/cpp/noreturn.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,43 @@ ms.assetid: 9c6517e5-22d7-4051-9974-3d2200ae4d1d
1010

1111
**Microsoft Specific**
1212

13-
This **`__declspec`** attribute tells the compiler that a function does not return. As a consequence, the compiler knows that the code following a call to a **`__declspec(noreturn)`** function is unreachable.
13+
The **`__declspec`** attribute tells the compiler that a function does not return. The compiler then knows that the code following a call to a **`__declspec(noreturn)`** function is unreachable.
1414

15-
If the compiler finds a function with a control path that does not return a value, it generates a warning (C4715) or error message (C2202). If the control path cannot be reached due to a function that never returns, you can use **`__declspec(noreturn)`** to prevent this warning or error.
15+
If the compiler finds a function with a control path that does not return a value, it generates a warning (C4715) or error message (C2202). If the control path cannot be reached due to a function that never returns, use **`__declspec(noreturn)`** to prevent this warning or error.
1616

1717
> [!NOTE]
1818
> Adding **`__declspec(noreturn)`** to a function that is expected to return can result in undefined behavior.
1919
2020
## Example
2121

22-
In the following sample, when the argument for `get_num` is negative, a call to `fatal` is made, but there is no return statement in that control path. Declaring `fatal` as **`__declspec(noreturn)`** avoids an error or warning message in `get_num`.
22+
In the following example, when the argument for `isZeroOrPositive` is negative, a call to `fatal` is made. There is no return statement in that control path, which results in warning C4715 that not all control paths return a value. Declaring `fatal` as **`__declspec(noreturn)`** mitigates that warning because there is no point in it since `fatal()` terminates the program.
2323

2424
```cpp
2525
// noreturn2.cpp
2626
#include <exception>
2727

28-
__declspec(noreturn) void fatal() {
28+
__declspec(noreturn) void fatal()
29+
{
2930
std::terminate();
3031
}
3132

32-
int get_num(int val) {
33-
if (val == 0) {
33+
int isZeroOrPositive(int val)
34+
{
35+
if (val == 0)
36+
{
3437
return 0;
3538
}
36-
else if (val > 0) {
39+
else if (val > 0)
40+
{
3741
return 1;
3842
}
39-
43+
// this function terminates if val is negative
4044
fatal();
4145
}
4246

43-
int main() {
44-
get_num(123);
47+
int main()
48+
{
49+
isZeroOrPositive(123);
4550
}
4651
```
4752

0 commit comments

Comments
 (0)