DEV Community

Aastha Gupta
Aastha Gupta

Posted on • Updated on

Quick Introduction to using in C++

If you've ever written a c++ code snippet, I predict that you've seen this particular line :

using namespace std;
Enter fullscreen mode Exit fullscreen mode

and thought to yourself, 'what does using mean?'

using is a keyword in C++ which is applicable in the following cases:

Bring all members from the namespace into​ the current scope.

The using directive allows all the names in a namespace to be used without the namespace-name as an explicit qualifier.

using namespace std; 
// brings all namespace names in the current scope
string my_string;
// std::string can now be referred as string throughout the scope

OR

std::string my_string; 
// string is now recognized, 
// however, will have to be addressed as std::string 
// wherever required
Enter fullscreen mode Exit fullscreen mode

So now you know what using does in :

using namespace std
Enter fullscreen mode Exit fullscreen mode

It brings namespace std in scope which makes it handy to use names in std without the explicit prefix std::

If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is, therefore, a good practice to not bring complete namespaces in scope in long code files to avoid such cases where an intuitive identifier name has to be dropped πŸ™ because same name exists in the namespace brought in scope by using. A workaround is to bring a specific name from a namespace in scope as:

using std::string;
Enter fullscreen mode Exit fullscreen mode

Bring a base class method ​or variable into the current class’ scope.

class BaseClass {
   public:
      void sayHi() {
         cout << "Hi there, I am Base" << endl;;
      }
};

class DerivedClass : BaseClass {
   public:
      using Base::sayHi; 
      void sayHi(string s) {
         cout << "Hi, " << s << endl;
         // Instead of recursing, the greet() method
         // of the base class is called.
         sayHi();
      }
};
Enter fullscreen mode Exit fullscreen mode

Create type aliases

template <typename T>
using MyName = MyVector<T, MyAlloc<T> >; 
MyName<int> p; // sample usage
Enter fullscreen mode Exit fullscreen mode
using flags = std::ios_base::fmtflags;
// identical to 
// typedef std::ios_base::fmtflags flags;
Enter fullscreen mode Exit fullscreen mode

typedef already exists in the C++ language which makes one wonder, why using was introduced to create type aliases. This is a valid question and an interesting one. There are a few interesting facts associated with typedef and using type aliases which you can read in this post πŸ˜„.

There are more caveats to using but are not covered here because this is a quick introduction, and also because of how rarely they are encountered. I encourage you to read more about using here.

Thanks for giving this article a read and I'll see you in the next one πŸ˜„

PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series here. I also answer why I don't use the series feature by dev.to there.

Top comments (2)

Collapse
 
pgradot profile image
Pierre Gradot

Since C++20, using can also be used with enumerated types: dev.to/pgradot/let-s-try-c-20-usin...

using Base::greet;

Did you mean sayHi() instead of greet()?

Bring a base class method

This is very useful to expose base class' constructors.

Collapse
 
guptaaastha profile image
Aastha Gupta

Hey Pierre, thanks for pointing out, I fixed it. I hope the article was useful to you !
using is used in multiple use cases now (one of which you pointed in the article). I encourage you to read more here if you want to know more.