Section 3.1. Namespace using Declarations


3.1. Namespace using Declarations

The programs we've seen so far have referred to names from the library by explicitly noting that the name comes from the std namespace. For example, when we want to read from the standard input, we write std::cin. Such names use the :: operator, which is the scope operator (Section 1.2.2, p. 8). This operator says that we should look for the name of the right-hand operand in the scope of the left-hand operand. Thus, std::cin says that we want the name cin that is defined in the namespace std. Referring to library names through this notation can be cumbersome.

Fortunately, there are easier ways to use namespace members. In this section we'll cover the safest mechanism: using declarations. We will see other ways to simplify the use of names from a namespace in Section 17.2 (p. 712).

A using declaration allows us to access a name from a namespace without the prefix namespace_name::. A using declaration has the following form:

      using namespace::name; 

Once the using declaration has been made, we can access name directly without reference to its namespace:

      #include <string>      #include <iostream>      // using declarations states our intent to use these names from the namespace std      using std::cin;      using std::string;      int main()      {       string s;       // ok: string is now a synonym for std::string       cin >> s;       // ok: cin is now a synonym for std::cin       cout << s;      // error: no using declaration; we must use full name       std::cout << s; // ok: explicitly use cout from namepsace std      } 

Using the unqualified version of a namespace name without a using declaration is an error, although some compilers may fail to detect this error.

A Separate Using Declaration Is Required for Each Name

A using declaration introduces only one namespace member at a time. It allows us to be very specific regarding which names are used in our programs. If we want to use several names from stdor any other namespacethen we must issue a using declaration for each name that we intend to use. For example, we could rewrite the addition program from page 6 as follows:

      #include <iostream>      // using declarations for names from the standard library      using std::cin;      using std::cout;      using std::endl;      int main()      {          cout << "Enter two numbers:" << endl;          int v1, v2;          cin >> v1 >> v2;          cout << "The sum of " << v1               << " and " << v2               << " is " << v1 + v2 << endl;          return 0;      } 

The using declarations for cin, cout, and endl mean that we can use those names without the std:: prefix, making the code easier to read.

From this point on, our examples will assume that using declarations have been provided for the names we use from the standard library. Thus, we will refer to cin, not std::cin, in the text and in code examples. To keep the code examples short, we won't show the using declarations that are needed to compile the examples. Similarly, our code examples will not show the necessary #include directives. Table A.1 (p. 810) in Appendix A lists the library names and corresponding headers for standard-library names we use in this primer.

Readers should be aware that they must add appropriate #include and using declarations to our examples before compiling them.



Class Definitions that Use Standard Library Types

There is one case in which we should always use the fully qualified library names: inside header files. The reason is that the contents of a header are copied into our program text by the preprocessor. When we #include a file, it is as if the exact text of the header is part of our file. If we place a using declaration within a header, it is equivalent to placing the same using declaration in every program that includes the header whether that program wants the using declaration or not.

In general, it is good practice for headers to define only what is strictly necessary.



Exercises Section 3.1

Exercise 3.1:

Rewrite the program from Section 2.3 (p. 43) that calculated the result of raising a given number to a given power to use appropriate using declarations rather than accessing library names through a std:: prefix.




C++ Primer
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2006
Pages: 223
Authors: Stephen Prata

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