The using-directive is often confused with a using-declaration. A using-declaration adds a given name to a local scope whereas a using-directive does not; it simply makes names accessible in the scope in which they were declared. For example:
namespace X { int i=0, j=0, k=0; }
int k=42; // global scope
// using-directive example:
void f1 (void) {
int i=0; // local scope
using namespace X; // make names from X accessible
i++; // local i
j++; // X::j
k++; // error: X::k or global k?
::k++; // global k
X::k++; // X::k
}
// using-declaration example:
void f2 (void) {
int i=0; // local scope
using X::i; // error: i declared twice!
using X::j; // ok
using X::k; // hides global k
i++;
j++; // X::j
k++; // X::k
}
Polluting the global namespace should always be avoided in non-trivial applications, however we often encounter tutorial code with an implied using namespace std; directive in the global scope to help the student focus on the actual code. We would never do this in production code. Ideally, the only name that should ever be visible in the global scope is the global main function. Everything else should ideally be put in their own namespaces and only imported into a local scope as and when they are actually needed. If we should require additional names to be accessible in the global scope, then these should be limited to just those names which represent truly global concepts.
Large programs tend to use a lot of namespaces and it can be tedious typing them out in full, particularly when they are nested or long and descriptive names. A using-declaration (at local scope) helps us to isolate just those names we are actually interested in, but if we need to use a lot of names from the same namespace, a using-directive helps cut down on the verbosity.
Namespace aliases are also useful for reducing long, descriptive names to a less verbose form in order to aid readability:
namespace American_Telephone_and_Telegraph { /* ... */ } // too long
// ...
namespace ATT = American_Telephone_and_Telegraph; // alias
You might ask why the programmer didn't simply use ATT in the first place, but short names greatly increase the risk of a name-clash. Thus it is better for the programmer to introduce shorthand aliases as and when they are actually required.
Naturally, we should avoid using-directives, using-declarations and namespace aliases in headers, because we cannot predict exactly where those names might be included, resulting in namespace pollution that can lead to some very obscure or subtle errors that would be difficult to track down.
what are the advantages and disadvantages of refrigeration
what are advantages and disadvantages of shapper machine
nomnjfjsd
there native indians created iron,titanium
None
what are the advantages and disadvantages of using a heligimbal
what are the advantages and disadvantages of voucher in business
what are the advantages and disadvantages of refrigeration
what is the advantages and disadvantages of quilted fabric
advantages and disadvantages of ring mouse
what are advantages and disadvantages of shapper machine
There are great advantages and disadvantages to using sales figures. Advantages to using sales figures is to help with projections of sale for future years. The disadvantages of using sales figures is the market is volatile.
What are all the advantages and disadvantages of using an electronic dairy?
There are several advantages and disadvantages of using 1 KG inorganic refrigerants. Some of the advantages and disadvantages are cost, energy efficiency, safety issues, and system issues.
disadvantages *not to scale *there are limitations
potato.
glen