blob: 09e33e0375e4040065561add8704c7891b26e388 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<h1 id="firstHeading" class="firstHeading">unreachable</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stddef.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">#define unreachable() /* see below */</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <p>The function-like macro <code>unreachable</code> expands to a <code>void</code> expression. Executing <code>unreachable()</code> results in <a href="../language/behavior" title="c/language/behavior">undefined behavior</a>.</p>
<p>An implementation may use this to optimize impossible code branches away (typically, in optimized builds) or to trap them to prevent further execution (typically, in debug builds).</p>
<h3 id="Possible_implementation"> Possible implementation</h3> <table class="eq-fun-cpp-table"> <tr> <td> <div class="c source-c"><pre data-language="c">// Uses compiler specific extensions if possible.
#ifdef __GNUC__ // GCC, Clang, ICC
#define unreachable() (__builtin_unreachable())
#elifdef _MSC_VER // MSVC
#define unreachable() (__assume(false))
#else
// Even if no extension is used, undefined behavior is still raised by
// the empty function body and the noreturn attribute.
// The external definition of unreachable_impl must be emitted in a separated TU
// due to the rule for inline functions in C.
[[noreturn]] inline void unreachable_impl() {}
#define unreachable() (unreachable_impl())
#endif</pre></div> </td>
</tr>
</table> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
struct Color { uint8_t r, g, b, a; };
struct ColorSpan { struct Color* data; size_t size; };
// Assume that only restricted set of texture caps is supported.
struct ColorSpan allocate_texture(size_t xy)
{
switch (xy)
{
case 128: [[fallthrough]];
case 256: [[fallthrough]];
case 512:
{
/* ... */
struct ColorSpan result = {
.data = malloc(xy * xy * sizeof(struct Color)),
.size = xy * xy
};
if (!result.data)
result.size = 0;
return result;
}
default:
unreachable();
}
}
int main(void)
{
struct ColorSpan tex = allocate_texture(128); // OK
assert(tex.size == 128 * 128);
struct ColorSpan badtex = allocate_texture(32); // Undefined behavior
free(badtex.data);
free(tex.data);
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">Segmentation fault</pre></div> </div> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/unreachable" title="cpp/utility/unreachable">C++ documentation</a></span> for <code>unreachable</code> </td>
</tr> </table> <h3 id="External_Links"> External Links</h3> <table> <tr style="vertical-align:top;"> <td>1. </td> <td>
<a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005funreachable">GCC docs: <code>__builtin_unreachable</code></a> </td>
</tr> <tr style="vertical-align:top;"> <td>2. </td> <td>
<a rel="nofollow" class="external text" href="https://clang.llvm.org/docs/LanguageExtensions.html#builtin-unreachable">Clang docs: <code>__builtin_unreachable</code></a> </td>
</tr> <tr style="vertical-align:top;"> <td>3. </td> <td>
<a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/cpp/intrinsics/assume">MSVC docs: <code>__assume</code></a> </td>
</tr>
</table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/program/unreachable" class="_attribution-link">https://en.cppreference.com/w/c/program/unreachable</a>
</p>
</div>
|