Declaring and initializing a variable in a Conditional or Control statement in C++

后端 未结 9 1437

In Stroustrup\'s The C++ Programming Language: Special Edition (3rd Ed), Stroustrup writes that the declaration and initialization of variables in the conditionals

9条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 11:40

    I've run into a similar problem:

    The problem seems to be the parentheses around the int declaration. It should work if you can express the assignment and test without them, i.e.

    if (int i = read(socket)) {
    

    should work, but that means the test is != 0, which is not what you want.

提交回复
热议问题