← Back to list

In C++, templates are a mechanism that allows you to create generic types and functions that can…

For example, you can create a template function that can add two numbers of any type like this:

Brian · 2023-05-08 21:26 · 21 claps · 2.2 min read
#c-tutorial-for-beginners #c-plus-plus-training #c-plus-plus-language #template #c-tutorial
Open on Medium ↗

Using the C++ Template and ‘Using’ Keywords to Create Type Aliases

In C++, templates are a mechanism that allows us to create generic types and functions that can work with a variety of data types without being re-written for each type. Templates are defined using the keyword “template” followed by one or more template parameters that can be used to specify the type or types the template will work with.

For example, we can create a template function that can add two numbers of any type like this:

template <typename T>
T add(T a, T b) {
    return a + b;
}

In this example, the template keyword indicates the start of a template definition. The typename keyword is used to specify a type parameter. In this case, the type parameter is named T.

In C++, we can also have multiple typename parameters in a template declaration. The syntax is as follows:

template<typename T1, typename T2>
class MyClass {
public:
    T1 val1;
    T2 val2;

    MyClass(T1 t1, T2 t2) : val1(t1), val2(t2) {}
};

In this example, we have a class MyClass that takes two typename parameters T1 and T2. We have two member variables val1 and val2 of type T1 and T2, respectively. We also have a constructor that takes two arguments of types T1 and T2.

We can use this template class to create objects with different types for T1 and T2. Here's an example:

MyClass<int, double> obj1(1, 2.0);
MyClass<std::string, char> obj2("hello", 'c');

In the first line, we create an object of type MyClass<int, double>, which has val1 of type int and val2 of type double. In the second line, we create an object of type MyClass<std::string, char>, which has val1 of type std::string and val2 of type char.

Furthermore, using the using keyword can help make our code more concise and readable when declaring complex types, especially when working with templates. It allows us to define a type alias that can be used in place of the full type declaration. This can make our code more modular and easier to maintain, while also reducing the risk of errors due to repetition or typos in long type names.

For example, consider the following template code that defines a container class that can hold a single value of any type:

template<typename T>
class Container {
public:
    Container(T value) : m_value(value) {}
    T get() const { return m_value; }
private:
    T m_value;
};

We can replace this template definition with a using alias definition as follows:

template<typename T>
using MyContainer = Container<T>;

This defines an alias MyContainer that is equivalent to Container<T>, where Container is the type alias defined earlier. Now, we can use MyContainer as a type in our code:


int main() {
    // Create a MyContainer object with an integer value of 42
    MyContainer<int> myContainer(42);

    // Use the get() method to retrieve the value
    std::cout << "Value: " << myContainer.get() << std::endl;

    return 0;
}

In summary, templates are a powerful feature in C++ that allow us to write generic code that can work with different types. However, defining and using templates can sometimes be cumbersome, especially when dealing with complex template parameters. Using the “using” keyword to create type aliases for template types can make our code more concise and easier to read. By defining an alias with “using”, we can use the alias in our code instead of the full template type, while still retaining all the functionality of the original template.

If you find this information useful, please consider supporting and following me for further updates.🙂


메타데이터
post_id
315bd285c9a8
slug
in-c-templates-are-a-mechanism-that-allows-you-to-create-generic-types-and-functions-that-can-315bd285c9a8
url
https://medium.com/@briankworld/in-c-templates-are-a-mechanism-that-allows-you-to-create-generic-types-and-functions-that-can-315bd285c9a8
canonical_url
https://medium.com/@briankworld/in-c-templates-are-a-mechanism-that-allows-you-to-create-generic-types-and-functions-that-can-315bd285c9a8
author_url
https://medium.com/@briankworld
status
ok
fetched_at
2026-08-27 01:46:39