Skip to content

Commit e6ead26

Browse files
authored
Merge pull request #4806 from Rageking8/improve-__declspec-noreturn-example
Improve `__declspec(noreturn)` example
2 parents 585e347 + 5102d7d commit e6ead26

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

docs/cpp/noreturn.md

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,49 @@ 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,the **`else`** clause does not contain a return statement. Declaring `fatal` as **`__declspec(noreturn)`** avoids an error or warning message.
22+
In the following example, when the argument for `isZeroOrPositive` is negative, `fatal` is called. There isn't a 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, which is desirable because there is no point in it since `fatal()` terminates the program.
2323

2424
```cpp
2525
// noreturn2.cpp
26-
__declspec(noreturn) extern void fatal () {}
27-
28-
int main() {
29-
if(1)
30-
return 1;
31-
else if(0)
32-
return 0;
33-
else
34-
fatal();
26+
#include <exception>
27+
28+
__declspec(noreturn) void fatal()
29+
{
30+
std::terminate();
31+
}
32+
33+
int isZeroOrPositive(int val)
34+
{
35+
if (val == 0)
36+
{
37+
return 0;
38+
}
39+
else if (val > 0)
40+
{
41+
return 1;
42+
}
43+
// this function terminates if val is negative
44+
fatal();
45+
}
46+
47+
int main()
48+
{
49+
isZeroOrPositive(123);
3550
}
3651
```
3752
3853
**END Microsoft Specific**
3954
4055
## See also
4156
42-
[__declspec](../cpp/declspec.md)<br/>
57+
[__declspec](../cpp/declspec.md)\
4358
[Keywords](../cpp/keywords-cpp.md)

0 commit comments

Comments
 (0)