In [namespace.udecl]/10 you have the following example:
namespace A {
int i;
}
namespace A1 {
using A::i;
using A::i; // OK: double declaration
}
void f() {
using A::i;
using A::i; // error: double declaration
}
This snippet compiles in clang.
Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions, or to introduce enumerators into namespaces, block, and class scopes (since C++20).
A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .
Yes, you can declare (but not define) a function multiple times in a single translation unit. And yes, include guards usually prevent this, but that is not their only purpose.
A declarative region is a place where names can be declared in. I.e. they can be declared in a block, a class body, or in the bodies of a namespace, etc. A scope is just some snippet of program text.
The first is a declaration inside a namespace, and the multiple using statements could happen frequently using #includes. The second is inside a definition of a function, and you would never do that unless you made a mistake. You can't define the same symbol twice either, for example, but you can declare several times.
The using statement is also more than just a declaration. It is a bit stronger, as it imports a function from one namespace to another. For example, it can pull a protected base class member function into a derived class, making it public. It's almost a definition by linkage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With