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 of if 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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -