Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The using keyword

Reference for C++ using: alias declarations, alias templates, using-declarations for base members, using enum, and why using namespace is risky in headers.

using appears in several unrelated-looking roles that share one idea: it introduces a name for something that already exists. An alias declaration (using Name = type;) gives a type a new name. A using-declaration brings one name from a namespace or base class into the current scope. A using-directive (using namespace x;) makes every name of a namespace visible for lookup. Since C++20, using enum brings the enumerators of an enumeration into scope.

Alias declarations are the modern replacement for typedef. They read left to right and, unlike typedef, they can be templates.

C++
using Seconds = long long;                 // alias declaration
template <typename T> using Vec = std::vector<T>;   // alias template
using std::string;                         // using-declaration
using namespace std::chrono_literals;      // using-directive

Try it

C++ Aliases and alias templates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Using-declarations in classes and using enum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
using identifier = type-id;                      // alias declaration
template <class T> using identifier = type-id;   // alias template
using nested-name::name;                         // using-declaration
using namespace nested-name;                     // using-directive
using enum enum-name;                            // C++20
Form Description
using A = T; A becomes another name for the type T. It is not a new type, so A and T are fully interchangeable.
template <...> using A = ...; A family of aliases. Partial or explicit specialization of an alias template is not allowed.
using Base::name; Makes name from a base class (or a namespace) usable in the current scope. In a class, it also unhides all base overloads of that name and can change the access level.
using namespace ns; Names of ns can be found by unqualified lookup as if declared in the nearest enclosing namespace that contains both.
using enum E; Introduces all enumerators of E (C++20).

Usage notes

  • Prefer using X = ... to typedef. Function-pointer and array aliases are far easier to read this way.
  • A function with the same name in a derived class hides every base overload. A using Base::f; declaration brings them back.
  • Inheriting constructors also use this syntax: using Base::Base; makes the base constructors available to the derived class.
  • A using-directive at namespace scope in a header leaks into every file that includes it, which can create ambiguities and break code later. Keep using namespace out of headers; inside a function body it is fine.
  • A using-declaration at namespace scope (using std::string;) is much safer than a directive, because it adds exactly one name.
  • using namespace std::literals; (or just std::chrono_literals) is the usual way to enable literal suffixes such as 10s and "text"sv.
  • Since C++17 one using-declaration may list several names (using std::cout, std::endl;) and may be a pack expansion: using Bases::operator()...; is how the "overloaded lambdas" helper for std::visit is built.
  • Aliases do not create distinct types for overloading: void f(Score) and void f(int) are the same function.