`using` — C++ Keyword
`using` — C++ Keyword
The using keyword in C++: type aliases, alias templates, using declarations, and using directives.
`using` — C++ Keyword
The using keyword in C++: type aliases, alias templates, using declarations, and using directives.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
usingHas several distinct uses: type alias declarations (C++11 replacement for typedef), alias templates, using declarations (bring names into scope), and using directives (using namespace).
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
#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
}
using over typedef: the alias-target type appears on the right, making complex types easier to read.using namespace std; in header files; it pollutes every including translation unit.usingint 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;
}