Type Aliases in C++
Take the following typedef:
typedef int MyInt;
This can be written using a type alias as follows:
using MyInt = int;
Type aliases are easier to read than typedefs in certain situations. They are especially useful in cases where the typedef becomes complicated, which is the case with typedefs for function pointers as seen in the previous section. For example, the following typedef defines a type for a pointer to a function, which returns an integer and accepts a char and a double as parameters:
typedef int (*FuncType)(char, double);
Admittedly, this typedef is a bit convoluted because the name FuncType is somewhere in the middle of it. Using a type alias, this can be written as follows:
using FuncType = int (*)(char, double);
After reading through this section, you might think that type aliases are nothing more than easier-to- read typedefs, but there is more. The problem with typedefs becomes apparent when you want to use them with templates.