jeudi 17 juin 2021

declaration of if-statement branch on chapter 9 of "The C++ Programming Language", errata?

Warning: this question is asked here before, but since I'm newbie, I can't comment on the accepted answer there to ask my question. So, I decided to post this as a new question.

It's given in TC++PL 4th edition 2013 that

A branch of it-statement cannot be just a declaration. If we need to introduce a name in a branch, it must be enclosed in a block (9.2). For example:

void f1(int i) {
  if(i)
    int x = i+2; // error: declaration of if-statement branch
}

But when I run above function on visual studio 2019 (C++14 standard), it works without giving any error. can (is) there be any errate on the book? what do you think? here Keith Thompson said "there was errata on the 4th edition, I looked at the 4th edition 2015 version in which it's been said

The scope of a declaration of a branch of an if-statement is just that branch. If we need to introduce a name in a branch, it must be enclosed in a block (§9.2)."

but keith didn't provide any source to verify it. It's the reason why I post the question once more to verify that problem. I expect you to answer the following questions:

  1. "A branch of it-statement cannot be just a declaration." is this correct? (I suspect it's not) (if this is errata on 2013 version, and it's corrected on 2015, please provide snippet of that part).
  2. is function f1 different from the one I posted? keith provided snippet of function f1 (that's function f3), claiming that function have the following form: (again, keith just claimed it, but didn't provide anything)
void f3(int i) {
  if(i)
    int x = i+2; 
  ++x; // error: x is not in scope.
}

I appreciate your answers.

Aucun commentaire:

Enregistrer un commentaire