Forms of “using” Keyword

The “using” keyword has two different forms:

  1. using namespace std; – This form will bring everything in the std namespace into your code. 
  2. using std::cout; or using std:: cin; – This form will only bring the cout object into your code.
  3. using std:: endl;– This form is used to make only specific names in a namespace available in the current namespace.
  4. You can also use the “using” keyword with your own namespaces.

Note: if you have a namespace called “mynamespace”, you could use it like this: 

using namespace mynamespace;

Or, if you only wanted to bring in the “myclass” object from that namespace, you could use this form: 

using mynamespace::myclass;

Using Keyword in C++ STL

The using keyword in C++ is a tool that allows developers to specify the use of a particular namespace. This is especially useful when working with large codebases or libraries where there may be many different namespaces in use. The using keyword can be used to specify the use of a single namespace, or multiple namespaces can be listed using the using keyword.

When using the using keyword, it is important to keep in mind that the specified namespace will take precedence over any other namespace that is in scope. This can lead to unexpected results if the code is not well-organized. For this reason, it is generally considered good practice to use the using keyword sparingly and only when absolutely necessary.

In addition to the using keyword, C++ also provides the using namespace directive. This directive can be used to specify the use of all namespaces in a particular library or codebase. The using namespace directive should be used with caution, as it can make code difficult to read and maintain. This can be especially useful when working with large codebases or when you want to make sure that your code will run in a specific environment.

Similar Reads

Use of “using” keyword in C++ STL

The using keyword can be used in the following ways:...

Forms of “using” Keyword

...

Limitation of “using” Keyword:

...