Is the following allowed by the standard?
#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 for conflicting declaration.
Is the following allowed by the standard?
#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 for conflicting declaration.
Its not much clear to me from the standard, but then, there is this written
section 7.1.6.4 auto specifier
A program that uses auto in a context not explicitly allowed in this section is ill-formed.
Better read the mentioned section of the standard for all the allowed contexts.
Considering this, I believe g++ is correct and clang is wrong. But I could be wrong, there could be some separate section in standard which might be implying this context, but I could not find it.
Edit answer: As mention in the comments. The problem in this case is that writting
external int a;
auto a = 3;
is the same as writting
external int a;
int a = 3;
that means you have a new definition of a and that causes an error.
First answer:
For my understanding this breaks parts of the One definition rule. Specifically, I mean the following rule (in reference to MISRA C++ 2008) that says that an identifier with external linkage should always have only one definition. In your example you have a definition in the current file(auto a = 3;
) and with external you also refer to a definition in another file.