[Q: using declaration forces the file scope only in header?]
No, not possible. There is no "file scope".
The C++ standard doesn't define a file scope, only a namespace
scope. The C standard does speak of "file scope", however, and
it is a generally accepted synonym (at least among those of us
with extensive C experience) for "global namespace scope" (also
called simply "global scope"). While it's probably better to
use the official C++ terminology when speaking about C++
(although stating that a variable declared static has "global
scope" bothers me some), "file scope" is a quite understandable
and frequently used synonym for "global scope".
There is only "translation unit scope".
Where did you find that one? There's no such thing in either C
nor C++.
And the translation unit consists of all the files the
compiler has to scan during the processing. Since you
#include the header in your C++ source file, they both become
the translation unit. You cannot declare un-using of the
namespace, so the effect of the 'using' declaration extends to
the _end_ of the translation unit in which it's encountered.
A using declaration obeys the same scope rules as anything else.
Formally, a using declaration declares a new name in the
declarative region in which it occurs; the new name is a synonym
for the name of the entity in the declaration. Thus, if I
write:
namespace A { class Toto {} ; }
namespace B { using A::Toto ; }
B::Toto becomes a synonym for A::Toto.
Note that an anonymous namespace is the equivalent of something
like:
namespace uniqueName { /* ... */ }
using namespace uniqueName ;
This means that given the above, any reference to type or foo
may find uniqueName::type or uniqueName::foo, which is a synonym
for B::type resp. B::foo.
No other type, so the type declared in the anonymous namespace
is found. So the function is:
void ::A::foo( ::B::type )
I think that there is a question with regards to the standard,
although I don't think it applies here. Formally, if you had
written something like:
inline void
foo()
{
type p ;
// ...
}
in namespace A, "type" binds to the name in the using
declaration in the anonymous namespace; i.e. to a different name
in each translation unit. And according to §3.2, that is a
violation of the one definition rule, and thus undefined
behavior (although I can hardly imagine an implementation in
which it didn't work).
Yes. Use macros that you can 'define' and 'undef'.
Or simply place the using declarations in namespace A. Or forgo
them entirely, declaring the foo in A:
void foo( B::type p ) {}
Or create a private namespace, according to some naming
convention. (I often use something like ClassNamePrivate, when
a class ClassName needs some additional, implementation specific
declarations at namespace scope. The namespace will be nested
in the same namespace as the class, and surrounded by
//!@implementation ... //!@end comments, so it won't show up in
the generated documentation. Presumable, that's sufficient for
the client programmer to realize that he's not supposed to use
anything in it.)