Skip to content

Improve __declspec(noreturn) example #4806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions docs/cpp/noreturn.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,49 @@ ms.assetid: 9c6517e5-22d7-4051-9974-3d2200ae4d1d

**Microsoft Specific**

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.
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.

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.
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.

> [!NOTE]
> Adding **`__declspec(noreturn)`** to a function that is expected to return can result in undefined behavior.

## Example

In the following sample,the **`else`** clause does not contain a return statement. Declaring `fatal` as **`__declspec(noreturn)`** avoids an error or warning message.
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.

```cpp
// noreturn2.cpp
__declspec(noreturn) extern void fatal () {}

int main() {
if(1)
return 1;
else if(0)
return 0;
else
fatal();
#include <exception>

__declspec(noreturn) void fatal()
{
std::terminate();
}

int isZeroOrPositive(int val)
{
if (val == 0)
{
return 0;
}
else if (val > 0)
{
return 1;
}
// this function terminates if val is negative
fatal();
}

int main()
{
isZeroOrPositive(123);
}
```

**END Microsoft Specific**

## See also

[__declspec](../cpp/declspec.md)<br/>
[__declspec](../cpp/declspec.md)\
[Keywords](../cpp/keywords-cpp.md)