Skip to content

Commit 45dd0fb

Browse files
author
mikeblome
committed
update per GH issue 366
1 parent 6a7bfe5 commit 45dd0fb

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/cpp/goto-statement-cpp.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ 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 by skipping over the initialization of any variable that is in scope in that location. The following example raises C2362:
30+
31+
```cpp
32+
33+
int bar(bool b)
34+
{
35+
if (!b)
36+
{
37+
goto exit; //C2362
38+
}
39+
else
40+
{ /*...*/ }
41+
42+
int error_code = 42;
43+
44+
exit:
45+
return error_code;
46+
}
47+
48+
```
2849
2950
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.
3051

0 commit comments

Comments
 (0)