Solution

I l @ ve RuBoard

graphics/bulb.gif

Although C++ changed greatly during standardization, few of the changes the committee made are likely to break existing code. There is one change, however, that will cause nearly all earlier C++ code to fail to compile ”the addition of namespaces and, particularly, the fact that the whole C++ standard library now lives in namespace std .

If you are a C++ programmer and this hasn't troubled you yet, it probably will soon, because the newest releases of most compilers have conformed to this change. As you start upgrading to the latest release of your compiler, or stop using legacy (and now nonstandard) pre-namespace headers, your code will be forced to adapt.

What are using-declarations and using-directives, and how are they used? Give examples . Which is order-dependent?

Using-Declarations

A using-declaration creates a local synonym for a name actually declared in another namespace. For the purposes of overloading and name resolution, a using-declaration works just like any declaration.

 // Example 39-1(a) // namespace A {   int f( int );   int i; } using A::f; int f( int ); int main() {   f( 1 );     // ambiguous: A::f() or ::f()?   int i;   using A::i; // error, double declaration (just as               //  writing "int i;" again would be) } 

A using-declaration brings in only names whose declarations have already been seen. For example:

 // Example 39-1(b) // namespace A {   class X {};   int Y();   int f( double ); } using A::X; using A::Y; // only function A::Y(), not class A::Y using A::f; // only A::f(double), not A::f(int) namespace A {   class Y {};   int f( int ); } int main() {   X x;      // OK, X is a synonym for A::X   Y y;      // error, A::Y not visible because the             //  using-declaration preceded the             //  actual declaration that's wanted   f( 1 );   // oops: this uses a silent implicit             //  conversion and calls A::f(double),             //  NOT A::f(int), because only the             //  first A::f() declaration had been              //  seen at the point of the using-              //  declaration for the name A::f } 

This feature makes using-declarations order-dependent, particularly when using names from a namespace split across a group of header files. The order of header inclusion, specifically which headers are included before and after the using-declaration, affects which names the using-declaration brings into scope. Of course, namespace std just happens to fit the description "a namespace split across a group of header files." More on this in the next Item.

Using-Directives

A using-directive allows all names in another namespace to be used in the scope of the using-directive. Unlike a using-declaration, a using-directive brings in names declared both before and after the using-directive. Of course, the name's declaration still has to be seen before the name can be used. For example:

 // Example 39-1(c) // namespace A {   class X {};   int f( double ); } void f() {   X x;      // OK, X is a synonym for A::X   Y y;      // error, no Y has been seen yet   f( 1 );   // OK, calls A::f(double) with parameter promotion } using namespace A; namespace A {   class Y {};   int f( int ); } int main() {   X x;      // OK, X is a synonym for A::X   Y y;      // OK, Y is a synonym for A::Y   f( 1 );   // OK, calls A::f(int) } 
I l @ ve RuBoard


More Exceptional C++
More Exceptional C++: 40 New Engineering Puzzles, Programming Problems, and Solutions
ISBN: 020170434X
EAN: 2147483647
Year: 2001
Pages: 118
Authors: Herb Sutter

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net