Is it possible to redefine a variable as automatic, which is displayed on the same type?

Is standard resolution allowed?

#include <iostream> extern int a; auto a = 3; int main(int, char**) { std::cout << a << std::endl; return 0; } 

clang accepts the code. g ++ complains about a conflicting declaration.

+16
c ++ language-lawyer c ++ 11 extern auto
May 24 '16 at 7:57 a.m.
source share
2 answers

It is not very clear to me from the standard, but then there is it written

section 7.1.6.4 auto-specifier
A program that uses auto in a context not explicitly permitted in this section is poorly formed.

It is better to read the mentioned section of the standard for all permitted contexts.

Given this, I believe g ++ is correct and clang is incorrect. But I could be wrong, there might be some separate section in the standard that might imply this context, but I could not find it.

+2
May 24 '16 at 8:31
source share

Edit answer: As mentioned in the comments. The problem in this case is that the spelling

 external int a; auto a = 3; 

is the same as spelling

 external int a; int a = 3; 

this means that you have a new definition of a and that is causing an error.

First answer: For my understanding, this violates parts of the Unified Definition rule. In particular, I have in mind the following rule (in relation to MISRA C ++ 2008), which states that an identifier with external communication should always have only one definition. In your example, you have a definition in the current file ( auto a = 3; ), and with the external you also refer to the definition in another file.

-2
May 24 '16 at 8:45
source share



All Articles