If you've ever written a c++ code snippet, I predict that you've seen this particular line :
using namespace std;
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
So now you know what using
does in :
using namespace std
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, thenamespace
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 thenamespace
brought in scope byusing
. A workaround is to bring a specific name from a namespace in scope as:
using std::string;
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();
}
};
Create type aliases
template <typename T>
using MyName = MyVector<T, MyAlloc<T> >;
MyName<int> p; // sample usage
using flags = std::ios_base::fmtflags;
// identical to
// typedef std::ios_base::fmtflags flags;
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 aboutusing
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)
Since C++20, using can also be used with enumerated types: dev.to/pgradot/let-s-try-c-20-usin...
Did you mean
sayHi()
instead ofgreet()
?This is very useful to expose base class' constructors.
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.