c++ - if statement without the inner scope? -
afaik, every pair of { }
in code creates new scope. if it's used sake of without if
, for
, function or other statement demands it:
void myfun(void) { int a; { int local; } }
i started wondering - when if
statement written without using braces (with 1-line body) still create new scope?
voidmyfun(int a) { int b; if (a == 1) int tmp; // 1 local if? else int tmp2; // or one? b = 2; // use tmp here? }
n4140 [stmt.select]/1 reads:
the substatement in selection-statement (each substatement, in
else
form ofif
statement) implicitly defines block scope
so, code
if (a == 1) int tmp; // 1 local if? else int tmp2; // or one?
is equivalent to
if (a == 1) { int tmp; // yes, 1 local if } else { int tmp2; // , 1 }
Comments
Post a Comment