How To Use Alias Templates For Traits In C++ 17 and Beyond
One of the great features of C++ is templates, they are parameterized by alias templates in C++11. Then, In C++14 and C++17, they improved C++11’s feature with a number of template aliases whose use simplifies the traits. This feature is called “Alias Templates For Traits” and in this post, we explain what is and alias template and how we can use alias templates with traits. What is a template In C++ ? A template is a simple and a very powerful statement in C++ which defines the operations of a class or function and lets the user apply the same template on different types in those operations. A template defines the operations of a class, a function, it is an alias. For example, we can create an add(a,b) function template as shown below. template T add (T a, T b) { return a+b; } What is a type alias, and an alias template in C++ 11? Type Alias is a term that refers to a previously defined types, alias declaration can be used to declare a name to use as a synonym for a previously declared type. We use using declaration (using-declaration) to declare a type alias, and it is effectively the same as typedef. This can be in block scope, class scope, or namespace scope. Type alias does not introduce a new type and it cannot change the usage or meaning of an existing type name. A type alias declaration is completely the same as typedef declaration. Type alias can be used to create an alias template that can be used as a custom allocator. Type alias which comes after C++11 standard, is used to create an alias template which can be used as a custom allocator. An alias template is an alias that uses a template which refers to a family of types. For example, let’s create a table template which has type, rows and cols parameters. We can create this table (my_table) template as below, // A Template Example template class my_table { }; We can use this template to create two more alias templates. Here we create a single column (my_column) and a single row (my_row) templates as below, // Alias Template Example template using my_column = my_table; template using my_row = my_table; As you see, we have a my_table template and my_column, my_row templates which are alias templates of the my_table template. These templates can be used to with any data types (int, float, char, string, wstring, etc.). Now we can use all the templates to create a table which has rows and columns or a single row or a single column data in a given type. Here is how we can use them, my_table table1; my_column col1; my_row row1; What are the alias templates for traits in C++ 14? In C++17, there are alias templates for type traits, they are defined in header and they can be used by using #include . In C++17, they improved C++11’s TransformationTraits feature with a number of template aliases whose use simplifies the traits. According to N3655 paper, “A TransformationTrait modifies a property of a type. It shall be a class template that takes one template-type argument and, optionally, additional arguments that help define the modification. It shall define a nested type1 named type, which shall be a synonym for the modified type.” In […]
