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.
Table of Contents
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.
1 2 3 4 5 6 7 |
template <class T> 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,
1 2 3 4 5 6 |
// A Template Example template <typename T, int rows, int cols> 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,
1 2 3 4 5 |
// Alias Template Example template <typename T, int rows> using my_column = my_table<T, rows, 1>; template <typename T, int cols> using my_row = my_table<T, 1, cols>; |
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,
1 2 3 4 5 |
my_table<int, 50, 10> table1; my_column<int, 20> col1; my_row<int, 80> 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 <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,
1 2 3 4 5 6 7 8 |
template <class T> using remove_const_t = typename remove_const<T>::type; template <class T> using remove_volatile_t = typename remove_volatile<T>::type; template <class T> using remove_cv_t = typename remove_cv<T>::type; template <class T> using add_const_t = typename add_const<T>::type; template <class T> using add_volatile_t = typename add_volatile<T>::type; template <class T> using add_cv_t = typename add_cv<T>::type; |
Reference modifications,
1 2 3 4 5 |
template <class T> using remove_reference_t = typename remove_reference<T>::type; template <class T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type; template <class T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type; |
Sign modifications,
1 2 3 4 |
template <class T> using make_signed_t = typename make_signed<T>::type; template <class T> using make_unsigned_t = typename make_unsigned<T>::type; |
Array modifications,
1 2 3 4 |
template <class T> using remove_extent_t = typename remove_extent<T>::type; template <class T> using remove_all_extents_t = typename remove_all_extents<T>::type; |
Pointer modifications,
1 2 3 4 |
template <class T> using remove_pointer_t = typename remove_pointer<T>::type; template <class T> using add_pointer_t = typename add_pointer<T>::type; |
Other transformations,
1 2 3 4 5 6 7 8 9 10 |
template <size_t Len, std::size_t Align=default-alignment> using aligned_storage_t = typename aligned_storage<Len,Align>::type; template <std::size_t Len, class... Types> using aligned_union_t = typename aligned_union<Len,Types...>::type; template <class T> using decay_t = typename decay<T>::type; template <bool b, class T=void> using enable_if_t = typename enable_if<b,T>::type; template <bool b, class T, class F> using conditional_t = typename conditional<b,T,F>::type; template <class... T> using common_type_t = typename common_type<T...>::type; template <class T> using underlying_type_t = typename underlying_type<T>::type; template <class T> using result_of_t = typename result_of<T>::type; |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
template<class _Ty> _INLINE_VAR _CONST_DATA bool is_void_v = is_void<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_null_pointer_v = is_null_pointer<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_integral_v =is_integral<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_floating_point_v = is_floating_point<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_array_v = is_array<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_pointer_v = is_pointer<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_lvalue_reference_v = is_lvalue_reference<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_rvalue_reference_v = is_rvalue_reference<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_member_object_pointer_v = is_member_object_pointer<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_member_function_pointer_v = is_member_function_pointer<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_enum_v = is_enum<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_union_v = is_union<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_class_v = is_class<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_function_v = is_function<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_reference_v = is_reference<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_arithmetic_v = is_arithmetic<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_fundamental_v = is_fundamental<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_object_v = is_object<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_scalar_v = is_scalar<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_compound_v = is_compound<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_member_pointer_v = is_member_pointer<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_const_v = is_const<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_volatile_v = is_volatile<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_trivial_v = is_trivial<_Ty>::value; template<class _Ty> _INLINE_VAR _CONST_DATA bool is_trivially_copyable_v = is_trivially_copyable<_Ty>::value; template<class _Ty>_INLINE_VAR _CONST_DATA bool is_standard_layout_v = is_standard_layout<_Ty>::value; |
Here is a simple example how we can use std::is_floating_point_v
in C++17 and beyond,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> #include <type_traits> #include <vector> int main() { if (std::is_floating_point_v< std::vector<float>::value_type>) std::cout << "Vector type is floating point"; system("pause"); return 0; } |
For more details about this feature in C++14 standard, please see this N3655
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition