What is the difference between "typedef" and "using" in C++11?

What is the difference between 'typedef' and 'using' in C++11?
If you are a C++ programmer, you have probably encountered the 'typedef' keyword used to create a type alias. However, with the introduction of C++11, a new keyword called 'using' emerged as an alternative way to achieve the same result. But what exactly is the difference between the two? Let's dive into it!
The Basics: 'typedef' and 'using'
In its simplest form, 'typedef' is used to create a new name for an existing type. For example:
typedef int MyInt;Here, we create a type alias 'MyInt' for the 'int' type. This allows us to use 'MyInt' instead of 'int' throughout our code.
On the other hand, 'using' can also be used to create type aliases, but with a slightly different syntax:
using MyInt = int;Again, we achieve the same result as with 'typedef', creating a type alias for 'int' called 'MyInt'. However, notice the difference in syntax. Instead of 'typedef', we use the 'using' keyword followed by an equals sign '='.
Template Typedefs: A New Syntax
One significant reason for the introduction of the 'using' keyword in C++11 was to provide a way to express "template typedefs." Let's see an example to understand this better:
template<class T> using MyType = AnotherType<T, MyAllocatorType>;In this example, we create a type alias 'MyType' that represents 'AnotherType' instantiated with the template parameters 'T' and 'MyAllocatorType'. This gives us the flexibility to create type aliases for templated types, which was not possible with 'typedef'.
Subtle Differences: Weak Aliasing vs. New Types
Now, let's address the main question: are there any other differences between 'typedef' and 'using' in the standard?
Initially, 'typedef' only creates a new name for an existing type, without creating a new distinct type. This means that conversions between the original type and the typedef name are implicit.
typedef int MyInt;
int i = 42;
MyInt j = i; // Implicit conversion from 'int' to 'MyInt'However, with 'using', the situation is different. 'using' creates a new type, not just a new name for an existing type. This means that conversions between the original type and the 'using' name are not implicit.
using MyInt = int;
int i = 42;
MyInt j = i; // Error! No implicit conversion between 'int' and 'MyInt'To perform the conversion, you need to use an explicit type cast:
using MyInt = int;
int i = 42;
MyInt j = static_cast<MyInt>(i); // Explicit conversion from 'int' to 'MyInt'Conclusion
In summary, both 'typedef' and 'using' can be used to create type aliases in C++11. However, 'typedef' only creates a new name for an existing type, while 'using' creates a new type. This distinction leads to differences in implicit conversions between the original type and the typedef/using name.
Understanding these differences is crucial when working with type aliases in C++. So, the next time you need to create a type alias, consider whether you need a weak aliasing or a new type, and choose between 'typedef' and 'using' accordingly.
Was this guide helpful? Do you have any more questions about 'typedef' and 'using' in C++11? Let us know in the comments below and keep the conversation going! 😄🚀
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.



