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.

QuentinUK
  • 2,997
  • 21
  • 20
xmh0511
  • 7,010
  • 1
  • 9
  • 36
  • `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

2 Answers2

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
  • 3
    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
0

If #1 and #2 do declare the same entity, then C++20 [basic.def.odr]/1 is violated since the same entity is being defined twice. If #1 and #2 do not declare the same entity, then C++20 [basic.scope.declarative]/4 (the corresponding paragraph in N4659 having been quoted by the OP) is violated. So in either case, the program is ill-formed (and in either case, a diagnostic is required).

But which one is it? Thanks to Declarations and where to find them being accepted into C++23, we finally have an answer. In the DIS, [basic.link]/8 states that

Two declarations of entities declare the same entity if, considering declarations of unnamed types to introduce their names for linkage purposes, if any (9.2.4, 9.7.1), they correspond (6.4.1), have the same target scope that is not a function or template parameter scope, and either

  • they appear in the same translation unit, or
  • they both declare names with module linkage and are attached to the same module, or
  • they both declare names with external linkage.

The two a's obviously appear in the same translation unit and have the same target scope. Do they correspond? [basic.scope.scope]/4 says yes: they correspond because they both introduce the same name (and none of the listed exceptions applies).

So a single variable named a is being defined twice, and that's why the program is ill-formed.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312