Skip to content

Commit 2449d5d

Browse files
committed
E.27: make example compile, closes #1622
1 parent 0439326 commit 2449d5d

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

CppCoreGuidelines.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# <a name="main"></a>C++ Core Guidelines
22

3-
July 3, 2020
3+
August 3, 2020
44

55

66
Editors:
@@ -16187,25 +16187,27 @@ This can be messy:
1618716187
{
1618816188
Gadget g1 = make_gadget(17);
1618916189
if (!g1.valid()) {
16190-
return {0, g1_error};
16190+
return {0, g1_error};
1619116191
}
1619216192

16193-
Gadget g2 = make_gadget(17);
16193+
Gadget g2 = make_gadget(31);
1619416194
if (!g2.valid()) {
16195-
cleanup(g1);
16196-
return {0, g2_error};
16195+
cleanup(g1);
16196+
return {0, g2_error};
1619716197
}
1619816198

1619916199
// ...
1620016200

1620116201
if (all_foobar(g1, g2)) {
16202-
cleanup(g1);
1620316202
cleanup(g2);
16203+
cleanup(g1);
1620416204
return {0, foobar_error};
16205+
}
16206+
1620516207
// ...
1620616208

16207-
cleanup(g1);
1620816209
cleanup(g2);
16210+
cleanup(g1);
1620916211
return {res, 0};
1621016212
}
1621116213

@@ -16215,31 +16217,35 @@ A not uncommon technique is to gather cleanup at the end of the function to avoi
1621516217
std::pair<int, error_indicator> user()
1621616218
{
1621716219
error_indicator err = 0;
16220+
int res = 0;
1621816221

1621916222
Gadget g1 = make_gadget(17);
1622016223
if (!g1.valid()) {
16221-
err = g1_error;
16222-
goto exit;
16224+
err = g1_error;
16225+
goto g1_exit;
1622316226
}
1622416227

1622516228
{
16226-
Gadget g2 = make_gadget(17);
16227-
if (!g2.valid()) {
16229+
Gadget g2 = make_gadget(31);
16230+
if (!g2.valid()) {
1622816231
err = g2_error;
16229-
goto exit;
16230-
}
16232+
goto g2_exit;
16233+
}
1623116234

16232-
if (all_foobar(g1, g2)) {
16233-
err = foobar_error;
16234-
goto exit;
16235-
}
16236-
// ...
16235+
if (all_foobar(g1, g2)) {
16236+
err = foobar_error;
16237+
goto g2_exit;
16238+
}
16239+
16240+
// ...
16241+
16242+
g2_exit:
16243+
if (g2.valid()) cleanup(g2);
1623716244
}
1623816245

16239-
exit:
16240-
if (g1.valid()) cleanup(g1);
16241-
if (g2.valid()) cleanup(g2);
16242-
return {res, err};
16246+
g1_exit:
16247+
if (g1.valid()) cleanup(g1);
16248+
return {res, err};
1624316249
}
1624416250

1624516251
The larger the function, the more tempting this technique becomes.

0 commit comments

Comments
 (0)