0

Consider the following example:

void foo()
{
    class A;
    class A;
}

int main()
{
    foo();
    return 0;
}

And it is work fine. Why such redecration of class type is permitted? I'm confused...

UPD: Please give me a quote from c++ spec where such redeclaration is permitted... if it possible.

3 Answers3

1

§ 3.1 A declaration (Clause 7) may introduce one or more names into a translation unit or redeclare names introduced by previous declarations.

Thats in the first clause.

The second clause specifies whats a declaration. I didnt copy it here because it includes quite a few examples.

Blaz Bratanic
  • 2,279
  • 12
  • 17
1

Declaration and Definition are two different concepts. You can declare a name any number of times you want in a translation unit. But the definition should appear only once. check this and this

Community
  • 1
  • 1
Rakib
  • 7,435
  • 7
  • 29
  • 45
1

Moreover, if each class A is declared in different cpp files as global variables the linker will raise a warn if you invoke g++ with "-warn-common"

spdilo
  • 23
  • 1
  • 4