What the using keyword does in C++
The using keyword in C++ has a few related jobs. It can bring a name from a namespace into the current scope, give an existing type a new name, or bring members from a base class into a derived class. Which job you get depends on how the line is written and where it sits.
| Form | What it does | Example |
|---|---|---|
| Using declaration | Brings one name into scope | using std::cout; |
| Using directive | Brings every name in a namespace into scope | using namespace std; |
| Type alias | Gives a type a new name | using Count = unsigned int; |
| Base member declaration | Brings a base class member into a derived class | using Base::show; |
| Inherited constructors | Makes the base class constructors available in a derived class | using Base::Base; |
| Using enum (C++20) | Brings the enumerators of an enum into scope | using enum Color; |
The sections below take each form in turn.
Bringing one name in with a using declaration
Most names in the standard library live in the std namespace, so the usual form is std::cout. A using declaration lets you drop the prefix for one name:
#include <iostream>
using std::cout;
int main() {
cout << "Hello, world\n";
}
Only cout is brought in. Other names, such as std::cin, still need the prefix unless you add a declaration for them too.
You can also put a using declaration inside a function. Then it applies only to that function:
void greet() {
using std::cout;
cout << "Hi\n";
}
Bringing a whole namespace in with a using directive
Many beginner tutorials use this form:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world\n";
}
using namespace std; makes every name in std usable without the prefix. It is short, which is why it shows up so often in small programs.
The cost is possible name clashes. If your code or a library declares a name that also exists in std, a call that uses only the unqualified name can become ambiguous, and the compiler will report an error. The more names you bring in, the more likely this becomes. For a closer look at that line, see c using namespace std.
Many style guides discourage using directives, especially in header files. A header is included by other files, so a using namespace line there affects every file that includes it. If you use a directive, keep it in a .cpp file or narrow it to a function.
Creating type aliases
A using alias gives an existing type a shorter or clearer name:
#include <string>
#include <vector>
using Count = unsigned int;
using Names = std::vector<std::string>;
Count total = 0;
Names people = {"Ana", "Ben"};
A function pointer is a case where the alias reads more easily:
using Callback = void (*)(int);
This declares Callback as a pointer to a function that takes an int and returns nothing. The older typedef form, typedef void (*Callback)(int);, means the same thing. using puts the new name first and then the type, so it reads from left to right. With typedef, the name sits in the middle of the type, which gets harder to read with complicated types. Both still work, and you will see typedef in older code.
Alias templates are another thing using can do. A typedef cannot take template parameters directly:
#include <vector>
template <typename T>
using Matrix = std::vector<std::vector<T>>;
Matrix<double> grid;
Bringing base class members into a derived class
When a derived class declares a function with the same name as a base class function, the derived version hides every base version with that name. A using declaration brings the base versions back:
#include <iostream>
struct Base {
void show(int x) { std::cout << "int: " << x << "\n"; }
};
struct Derived : Base {
using Base::show;
void show(const char* text) { std::cout << "text: " << text << "\n"; }
};
int main() {
Derived d;
d.show(5); // int: 5
d.show("hello"); // text: hello
}
Without using Base::show;, the call d.show(5) would not compile, because Derived::show hides Base::show.
The rules for base and derived classes are covered in inheritance in c. The base member must also be accessible from the derived class. Access modifiers in c explains the public, protected and private levels.
Inheriting constructors
A derived class does not normally inherit its base class constructors. The using form makes them available:
#include <iostream>
struct Base {
Base(int value) : id(value) {}
int id;
};
struct Derived : Base {
using Base::Base; // inherits Base(int)
};
int main() {
Derived d(42);
std::cout << d.id << "\n"; // prints: 42
}
This is a C++11 feature. The base class’s default, copy and move constructors are not inherited.
Using enum in C++20
C++20 added using enum, which brings the enumerators of an enum into the current scope:
enum class Color { Red, Green, Blue };
Color pick() {
using enum Color;
return Green; // same as Color::Green
}
Compile in C++20 mode or later, for example with g++ -std=c++20. If enums are new to you, enum example in c walks through the basics.
Good habits
- Prefer a using declaration for the specific names you need, such as
using std::string;, over a using directive for a whole namespace. - Keep
using namespaceout of header files. For how to keep a header from being included twice, see header guard in c. - If you do write a using directive, keep its scope narrow, such as inside one function.
- Many programmers use
usingfor type aliases in new code. You will still seetypedefin older code, and both work.
Common questions
Is using the same as #include? No. #include brings in declarations from a header file. using controls which names you can write without a namespace prefix once those declarations are available.
Does C have a using keyword? No. The examples here are C++ and will not compile as C. C has no namespaces, and it uses typedef for type aliases.
How do I give a namespace a shorter name? Use a namespace alias, which uses the namespace keyword instead of using: namespace fs = std::filesystem;
Quick recap
using std::name;brings one name into scope.using namespace std;brings in a whole namespace. It is easy to write, but it can cause name clashes.using Alias = Type;gives a type a new name, and alias templates work too.using Base::member;inside a class brings base class members into the derived class.using enum Color;(C++20) brings enumerators into scope.