2
int main(){
   int a = 0; //#1
   int a = 1;  //#2
}

Consider the above code,I only find some quotes related to the question is,
[basic.scope.declarative]

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name:
1. they shall all refer to the same entity ,or all refer to functions and function templates;

Is the above quote a interpretation for why the program is ill-formed If more than one declaration declare the same name that denote the variable.If it's not,please correct me with some quotes that interpret why this situation is ill-formed.

xmh0511
  • 6,308
  • 1
  • 7
  • 33
  • `int a = 0;` is a definition (and declaration), not a declaration. related: https://stackoverflow.com/questions/33163028/declaration-vs-definition-in-c – mch May 07 '20 at 08:00
  • That defines two variables with the same name. It does not declare the same variable twice. – molbdnilo May 07 '20 at 08:01
  • Well, yes, that quote is the reason that code is ill-formed. What makes you think it might not be? There may well be other clauses in the standard which give a consistent outcome, but (hypothetically) if all those other rules were removed, the one you quote will make still mean the code is ill-formed. – Peter May 07 '20 at 08:04
  • @Peter Because,thers's another rules to determin whether a declaration is refer to **the same entity**,and I can't determin – xmh0511 May 07 '20 at 08:07
  • @mch A declaration is also a definition unless [[basic.def]](https://timsong-cpp.github.io/cppwp/n4659/basic.def#2),So I general say this is a declaration – xmh0511 May 07 '20 at 08:23
  • It is a declaration and definition, a duplicated declaration is fine, but the definition must be unique, therefore it is an error. – mch May 07 '20 at 08:34
  • @mch yes,you are right,however I want to use some standard rules to interpret this. – xmh0511 May 07 '20 at 08:36
  • The C++ standard is an ISO standard. Definitions in ISO standards are given by the standard itself (e.g. the C++ standard states a definition of "undefined"), by reference to another standard or specification, or by reference to "official language" - the common language in which the standard is written. The "official language" in the C++ standard is English. Since the C++ standard doesn't define the meaning of "the same entity", and doesn't refer to another standard for the meaning of that term, the meaning is consistent with dictionary and grammatical structure of English. – Peter May 07 '20 at 13:10
  • @Peter could you left an answers for this question,thanks – xmh0511 May 07 '20 at 13:45

1 Answers1

0

Because this causes a naming conflict. The two variables reference different mamory locations but share the same identifier(name) and the compiler is thus unable to differantiate between the two when you reference one of them in your program. Keep in mind that it is not a matter of bad practice to create naming conflicts such as this, it is against the syntax of C++ and causes a compile-time error.

Foivoschr
  • 455
  • 1
  • 4
  • 14
  • 2
    Welcome to StackOverflow! Questions with tag [tag:language-lawyer] expect answers based on standard or other documents relevant to given language. While your answer is not wrong, the question doesn't ask "Why it doesn't compile?", but rather "Where the C++ standard says that this shouldn't compile?". – Yksisarvinen May 07 '20 at 08:52