Return value from local scope?

后端 未结 6 1057
终归单人心
终归单人心 2021-01-05 11:06

Bumped into some code like this in our code base... which made me worried.

int foo(int a); // Forward declaration.

int baz() {
    int result = {
         i         


        
6条回答
  •  灰色年华
    2021-01-05 11:21

    You'd have to consult your compiler documentation. This construct is not allowed in standard C or standard C++.

    It's trivial to clean this up however, e.g.

    int baz()
    {
        int result;
        {
             int a = dosomestuff();
             result = foo(a)? 0: -1;
        }
        return result;
    }
    

提交回复
热议问题