`using` — C++ Keyword

`using` — C++ Keyword

The using keyword in C++: type aliases, alias templates, using declarations, and using directives.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

using

Has several distinct uses: type alias declarations (C++11 replacement for typedef), alias templates, using declarations (bring names into scope), and using directives (using namespace).

Syntax

using Alias = Type;                          // type alias
template <typename T> using Alias = Type<T>; // alias template

using Base::member;                          // using declaration in class
using namespace ns;                          // using directive

Example

#include <print>
#include <vector>
#include <map>

// Type alias
using Bytes = std::vector<unsigned char>;

// Alias template
template <typename K>
using StringMap = std::map<K, std::string>;

struct Base {
    void greet() const { std::println("Hello from Base"); }
};

struct Derived : Base {
    using Base::greet;   // bring Base::greet into Derived's overload set
};

int main() {
    Bytes data = {0x01, 0x02};
    std::println("{}", data.size());  // 2

    StringMap<int> m = {{1, "one"}, {2, "two"}};
    std::println("{}", m[1]);   // one

    Derived d;
    d.greet();   // Hello from Base
}

Notes

Example in practice

int main() {
    // Pick one facility from this reference page.
    // Write the smallest program that exercises its main precondition,
    // complexity rule, or lifetime constraint before scaling up.
    return 0;
}