Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Type alias declaration with using

The declaration using AliasName = Type; gives a type another name without creating a distinct type. Learn its equivalence to typedef and how const applies to pointer aliases.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

using AliasName = Type;

A type alias declaration (using type alias) has the form using AliasName = Type;. The name after using denotes the type written after =; a semicolon ends the declaration. For example, using Count = int; lets you use Count instead of int.

An alias has the same semantics as a name introduced by typedef: it does not create a new, distinct type. using Count = int; corresponds to typedef int Count;. An alias alone therefore does not make the type system distinguish conceptually different values.

The const qualifier applies to the complete type denoted by the alias. With using IntPointer = int*;, const IntPointer means int* const, not const int*. In the example, the address stored in pointer cannot change, but the pointed-to object count can change.

Examples

#include <iostream>
#include <type_traits>

using Count = int;
using IntPointer = int*;

static_assert(std::is_same_v<Count, int>);
static_assert(std::is_same_v<const IntPointer, int* const>);

int main() {
    Count count = 3;
    const IntPointer pointer = &count;
    *pointer += 2;
    std::cout << count << '\n';
}

Result

5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources