Use of “using” keyword in C++ STL

The using keyword can be used in the following ways:

  1. Using for namespaces
  2. Using for inheritance
  3. Using for aliasing
  4. Using for directives

1. “using” for namespaces

Using allows you to specify that you want to use a particular namespace. This is useful if you want to avoid typing out the full namespace name every time you want to use something from that namespace.

Example:

C++




#include <iostream>
using namespace std;
 
int main() {
   cout << "Hello, world!";
   return 0;
}


Output

Hello, world!

2. “using” for inheritance

Using allows you to specify that a class inherits from another class. This is useful if you want to avoid having to type out the full name of the base class every time you want to use it.

Example:

C++




#include <iostream>
using namespace std;
 
class Base {
public:
    int x;
 
    // parameterized constructor
    Base(int a) : x(a){};
};
 
class Derived : public Base {
public:
    int y;
    using Base::Base;
};
 
int main()
{
 
    Derived d(42);
    d.y = 12;
    cout << d.x << " " << d.y << '\n';
 
    return 0;
}


Output

42 12

Note: The C++ compiler does not create any default and copy constructor once we explicitly define any constructor. So we need to declare them explicitly to avoid bugs.

3. “using” for aliasing:

Using allows you to specify an alternate name for a type. This can be useful if you want to avoid having to type out the full name of a type every time you want to use it.

Example:

C++




#include <iostream>
using namespace std;
using ll =  long long; // Here we alias "ll" to stand for long long and save typing it out
 
int main() {
   
   ll a = 5;
   cout << a;
   return 0;
}


Output

5

4. “using for directives:

Using can be used as a directive to the compiler to tell it to include a particular header file. This is useful if you want to make sure that a particular header file is always included when you compile your code.

C++




#include <iostream>
using std::cout;
using std::endl;
 
int main() {
   cout << "Hello, world!" << endl;
   return 0;
}


Output

Hello, world!

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:

...