CKTIS / C++
using — type aliases and making names available
The using keyword has several distinct uses: it creates type aliases, makes selected declarations available, affects namespace name lookup, and exposes enumerators. Distinguish these forms: not every using declaration creates an alias.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
using Name = Type;
template<class T> using Alias = TypeExpression;
using ns::name;
using namespace ns;
using Base::member;
using Base::Base;
using enum EnumType;using Name = Type; declares a type alias, not a new, distinct type. In this respect it has the semantics of typedef. Writing template<class T> using Ptr = T*; creates an alias template: Ptr<int> means int*. A type alias is not a variable alias or a namespace alias.
using ns::name; makes selected declarations available as name, without the ns:: qualifier. It can expose the function overload set found at that point; overloads added later are not automatically included. In a derived class, using Base::member; can expose a base-class member, including hidden overloads. using Base::Base; is used to inherit constructors from a direct base class.
using namespace ns; is a directive affecting name lookup, not an alias declaration or a copy of declarations into the current scope. It may appear at namespace or block scope, but not class scope. Later declarations in ns can also be found through it. To reduce ambiguity risks, prefer qualified names or narrow using declarations, especially in headers.
using enum EnumType; makes each enumerator of the specified enumeration type available, allowing ready instead of State::ready. It does not change the enumeration type or its conversion rules. Conflicting enumerator names can cause an error. Merely writing using ns::EnumType; exposes the type name, not all its enumerators.
This entry targets C++23. The supplied source is an evolving working draft, not a frozen C++23 publication; the syntax shown does not use its later extensions. The example demonstrates an alias, an alias template, a using declaration, and using enum; its expected output is 3 1 followed by a newline.
Examples
#include <iostream>
#include <type_traits>
using Count = int;
template<class T> using Ptr = T*;
enum class State { idle, ready };
int main() {
using std::cout;
using enum State;
static_assert(std::is_same_v<Count, int>);
Count count = 3;
Ptr<Count> p = &count;
State state = ready;
cout << *p << ' ' << (state == State::ready) << '\n';
}
Result
3 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.