Skip to content

Commit cef4cdc

Browse files
authored
Merge pull request #1245 from mikeblome/mb-goto
update to goto topic per GH issue 366
2 parents 3452428 + de75ff8 commit cef4cdc

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

docs/cpp/goto-statement-cpp.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@ goto identifier;
2525
The labeled statement designated by `identifier` must be in the current function. All `identifier` names are members of an internal namespace and therefore do not interfere with other identifiers.
2626

2727
A statement label is meaningful only to a **goto** statement; otherwise, statement labels are ignored. Labels cannot be redeclared.
28+
29+
A **goto** statement is not allowed to transfer control to a location that skips over the initialization of any variable that is in scope in that location. The following example raises C2362:
30+
31+
```cpp
32+
int goto_fn(bool b)
33+
{
34+
if (!b)
35+
{
36+
goto exit; // C2362
37+
}
38+
else
39+
{ /*...*/ }
40+
41+
int error_code = 42;
42+
43+
exit:
44+
return error_code;
45+
}
46+
```
2847
2948
It is good programming style to use the **break**, **continue**, and **return** statements instead of the **goto** statement whenever possible. However, because the **break** statement exits from only one level of a loop, you might have to use a **goto** statement to exit a deeply nested loop.
3049
@@ -76,4 +95,4 @@ Jumped to stop. i = 3
7695

7796
## See also
7897
[Jump Statements](../cpp/jump-statements-cpp.md)
79-
[Keywords](../cpp/keywords-cpp.md)
98+
[Keywords](../cpp/keywords-cpp.md)

0 commit comments

Comments
 (0)