C++C++11C++14C++17C++20Learn C++

How To Use Alias Templates For Traits In C++ 17 and Beyond

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++ ?

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.

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,

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,

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,

What are the alias templates for traits in C++ 14?

In C++17, there are alias templates for type traits, they are defined in <type_traits> header and they can be used by using #include <type_traits>.

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 C++, a type trait, or in another term, a TransformationTrait is a class template that defines a transformation of its template type, there are many alias templates. According to this N3655 paper, here are some of them.

Const-volatile modifications,

Reference modifications,

Sign modifications,

Array modifications,

Pointer modifications,

Other transformations,

These are only some of type traits, there are many new additions. Note that, In the type_traits header, there are aliases with  _t  suffix that were introduced in C++14, and the aliases with _v suffix ones that were added in C++17.

How To Use Alias Templates For Traits In C++ 17?

In C++17, there are some more useful alias templates with _v suffix, here are some of examples from the <type_traits> header of C++ Builder 12,

Here is a simple example how we can use std::is_floating_point_v in C++17 and beyond,

For more details about this feature in C++14 standard, please see this N3655

How To Use Alias Templates For Traits In C++ 17 and Beyond

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++