1

Here is the code which causes a compile-time error:

#include <iostream>

int a;

void f()
{
    using ::a;
    using ::a; //'a' is already declared in this scope. 
}

int main(){ }

DEMO

What the standard says is (N4296::7.3.3/10 [namespace.udecl]):

A using-declaration is a declaration and can therefore be used repeatedly where (and only where) multiple declarations are allowed.

So, I supposed that any redeclaration is ill-formed in the block scope. But the program

#include <iostream>

void f()
{
    extern int a;
    extern int a;
}

int main(){ }

DEMO

works fine. So why is the using redeclaration actually prevented from appearing twice in the block scope?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • The "... can therefore be used repeatedly" doesn't necessarily imply that you can use the same name repeatedly. It just means you can write several `using ...;` statements at the beginning of the block. – Rufflewind Jan 16 '15 at 05:20
  • @Rufflewind Yes, but it doesn't answer my question because it's still unclear why we can't redeclare a name by a using declaration in the same scope. –  Jan 16 '15 at 05:23
  • The official standard actually gives an *explicit example* that [forbids what you are doing right now](https://stackoverflow.com/q/4252451), but not all compilers detect it and it is a bit inconsistent with the rest of the language, as stated in [this defect report on C++ standard](http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#36). – Rufflewind Jan 16 '15 at 05:24
  • Extern can be declared as many times as you want – Ankur Jan 16 '15 at 05:24
  • 1
    Is there a reason not to close this as a dup of http://stackoverflow.com/q/4252451 as suggested by @Rufflewind? – Jonathan Leffler Jan 16 '15 at 05:27
  • @JonathanLeffler Yes, now it's perectly clear. –  Jan 16 '15 at 05:27

0 Answers0