Skip to content

Commit a409df5

Browse files
authored
Improve __declspec(noreturn) example
1 parent 02e1daf commit a409df5

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

docs/cpp/noreturn.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,35 @@ If the compiler finds a function with a control path that does not return a valu
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 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`.
2323

2424
```cpp
2525
// noreturn2.cpp
26-
__declspec(noreturn) extern void fatal () {}
26+
#include <exception>
27+
28+
__declspec(noreturn) void fatal() {
29+
std::terminate();
30+
}
31+
32+
int get_num(int val) {
33+
if (val == 0) {
34+
return 0;
35+
}
36+
else if (val > 0) {
37+
return 1;
38+
}
39+
40+
fatal();
41+
}
2742

2843
int main() {
29-
if(1)
30-
return 1;
31-
else if(0)
32-
return 0;
33-
else
34-
fatal();
44+
get_num(123);
3545
}
3646
```
3747
3848
**END Microsoft Specific**
3949
4050
## See also
4151
42-
[__declspec](../cpp/declspec.md)<br/>
52+
[__declspec](../cpp/declspec.md)\
4353
[Keywords](../cpp/keywords-cpp.md)

0 commit comments

Comments
 (0)