
David A. answered 10/11/19
Experienced C++ tutor who loves to mentor and pass on my knowledge
Both using and typedef are equivalent, in that they both declare a type alias.
typedef example:
using example:
Both are identical aliases... so why use the using keyword, as opposed to typedef?
The benefit of using becomes very useful for alias templates. In the example above, the aliases are set to the type that we defined; in this case, the int data type. We can use the using keyword to declare a template alias, which allows the underlying type to change ( it's why we love templates so much :)
Let's look at a full example:
Output:
0
1
2
3
4
0
1.5
2.25
3.1415
5
Notice how the using keyword allowed us to define a type alias (Iterator) that allowed the underlying type to change; in this case, to be defined as an iterator of integers and then as an iterator of doubles.
I hope this helps!