0

As specified into the standard int a belongs to the simple declaration. Actually

simple-declaration:
    decl-specifier-seq_opt init-declarator-list_opt ; //
    attribute-specifier-seq decl-specifier-seq_opt init-declarator-list ;
type-specifier:
    trailing-type-specifier //
    class-specifier
    enum-specifier
trailing-type-specifier:
    simple-type-specifier //
    elaborated-type-specifier
    typename-specifier
    cv-qualifier
simple-type-specifier:
    nested-name-specifieropt type-name
    nested-name-specifier template simple-template-id
    char
    char16_t
    char32_t
    wchar_t
    bool
    short
    int //
    long
    signed
    unsigned
    float
    double
    void
    auto
    decltype-specifier

Hence int a is a simple declaration. But if we redeclare a into the same scope as the following:

int a;
int a;

We have

test.cpp:4:5: error: redefinition of ‘int a’
test.cpp:3:5: error: ‘int a’ previously declared here

So what exactly int a is?

  • 6
    What part of the standard says that you are allowed to perform two declarations of the same variable in the same scope? – merlin2011 May 14 '14 at 06:10
  • 2
    @merlin2011 I think the question is, what makes `int a;` a *definition* in this context. It has to be a *definition* to break the ODR rule. – juanchopanza May 14 '14 at 06:12
  • @user2357112 I don't understand, does ODR applied in this case? –  May 14 '14 at 06:13
  • Its a simple programming logic if you want to store integer value you have to declare a variable to create a memory location for this. now the location is identified by variable "a". So now you are not able to declare the same variable for new int memory location. – Jitendra Pareek May 14 '14 at 06:13
  • 1
    @JitendraPareek I don't interested in programming in this question. I want to find formal rules into the standard. –  May 14 '14 at 06:15
  • See also:http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration – Ferdinand Beyer May 14 '14 at 06:23

4 Answers4

9

They are each syntactically valid declarations, however the two together violate of the one definition rule. The compiler detects and reports this violation. (They are both declarations and definitions.)

David Schwartz
  • 166,415
  • 16
  • 184
  • 259
6

From the standard

A declaration is a definition unless it declares a function without specifying the function’s body

a is not a method, so int a means a declaration and definition. And if you define a name more than once in a single translation unit, you are violating One definition Rule, hence the error.

EDIT

For clarification, I am posting the whole paragraph:

A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (27) (7.5) and neither an initializer nor a function-body, it declares a static data member in a class definition (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3),a using-declaration (7.3.3), or a using-directive(7.3.4).

juanchopanza
  • 210,243
  • 27
  • 363
  • 452
Rakib
  • 6,975
  • 6
  • 24
  • 43
0

It is same as two persons having same name. the compiler confuses which one to refer. So, it is not allowed in same scope. Having two variables with same name is confusing for the compiler.

Varij Kapil
  • 103
  • 11
  • The counter example is the redeclaration as extern int a; extern int a; –  May 14 '14 at 06:46
0

we use declarations to declare variable names and types as well as to define memory for them. Most of the time these two actions occur at the same time, that is, most declarations are definitions (In case of variables and not functions). Hence the error. However this may not always be a case.

Deva
  • 48
  • 6