> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/05.-using-type-alias.md).

# 05. using (Type Alias)

`using` is the **modern (C++11+) way** to create a type alias — the same job `typedef` does, just with cleaner syntax.

```cpp
#include <iostream>

using text_t = std::string;

int main() {
    text_t firstName = "Bro";

    std::cout << firstName << '\n';

    return 0;
}
```

**Syntax:** `using NewName = ExistingType;`

Compare that to the old way:

```cpp
typedef std::string text_t;   // old style, reads right-to-left
using text_t = std::string;   // modern style, reads left-to-right: "text_t IS std::string"
```

***

### Why `using` is good (and preferred over `typedef`)

1. **Reads more naturally.** `using text_t = std::string;` reads left-to-right: "`text_t` is `std::string`." `typedef std::string text_t;` reads backwards and gets confusing with complex types.
2. **Handles templates — `typedef` can't.** This is the big one. You **cannot** create a templated `typedef`, but you **can** create a templated `using` alias:

   ```cpp
   // ❌ This does NOT work with typedef — no such thing as a "template typedef"
   // typedef std::vector<T> vec_t; // T is not defined here, error

   // ✅ This works perfectly with using
   template <typename T>
   using vec_t = std::vector<T>;

   int main() {
       vec_t<int> numbers = {1, 2, 3};
       vec_t<std::string> names = {"Alice", "Bob"};
   }
   ```
3. **Consistent with other modern C++ syntax** (like `auto`, lambdas), so it fits better in newer codebases.
4. **Same performance, same result** — for simple aliases, `using` and `typedef` compile to the exact same thing. It's purely about readability and template support.

***

### More cool examples

#### 1. Aliasing simple types

```cpp
#include <iostream>

using age_t = int;
using price_t = double;

int main() {
    age_t age = 21;
    price_t price = 499.99;

    std::cout << "Age: " << age << ", Price: $" << price << '\n';

    return 0;
}
```

#### 2. Aliasing a long container type

```cpp
#include <iostream>
#include <vector>
#include <string>

using nameList_t = std::vector<std::string>;

int main() {
    nameList_t players = {"Messi", "Ronaldo", "Neymar"};

    for (const auto& name : players) {
        std::cout << name << '\n';
    }

    return 0;
}
```

#### 3. A templated alias (the thing `typedef` can't do)

```cpp
#include <iostream>
#include <vector>

template <typename T>
using list_t = std::vector<T>;

int main() {
    list_t<int> scores = {90, 85, 100};
    list_t<std::string> names = {"Alice", "Bob"};

    std::cout << "First score: " << scores[0] << '\n';
    std::cout << "First name: " << names[0] << '\n';

    return 0;
}
```

#### 4. Aliasing a function pointer type

```cpp
#include <iostream>

using operation_t = int(*)(int, int);

int add(int a, int b) { return a + b; }

int main() {
    operation_t op = add;
    std::cout << op(2, 3) << '\n';

    return 0;
}
```

***

### Quick Recap

* `using NewName = ExistingType;` — the modern type-alias syntax.
* Does the same job as `typedef`, but reads more clearly.
* Unlike `typedef`, `using` supports **templated aliases** — a major reason it's preferred today.
* For new C++ code, reach for `using` over `typedef`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/05.-using-type-alias.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
