Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a variable be redeclared as auto that deduced to the same type? [duplicate]

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.

like image 752
Jamboree Avatar asked May 24 '16 07:05

Jamboree


1 Answers

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.

like image 64
Arunmu Avatar answered Sep 21 '22 06:09

Arunmu