EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C++ using vs typedef

By Yashi GoyalYashi Goyal

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » C++ using vs typedef

C++ using vs typedef

Definition on C++ using vs typedef

In C++, ‘using’ and ‘typedef’ performs the same task of declaring the type alias. There is no major difference between the two. ‘Using’ in C++ is considered to define the type synonyms. This method is also known as alias- declaration. Defining these alias-declaration works similar to defining the variables in C++ with ‘using’ statement. It helps in improving the overall readability of the code. The ‘using’ in C++ has the ability to create the alias-templates providing the ability for the underlying type, unlike the ‘typedef’ statement. With the use of ‘using’ in C++ code, one can have the usual type aliasing with the same level of abstraction and can specify the template parameters in the future.

‘Typedef’ in C++ performs a similar task of defining the alias. It basically introduces a name that becomes the synonym of the given type using the type declaration within that scope. One of the important features of ‘typedef’ is that it allows the programmer to encapsulate the details of implementation that may change over time. In C++, typedef provides all the facilities like the ‘using’ statement except it does not allow to work with templates which are very useful for the coding perspective. ‘typedef’ declaration does not introduce new types unlike union, enum, class, etc. Instead, they give new names to the already existing type. These new names occupy the same namespace as other identifiers.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Head to Head Comparison Between C++ using vs typedef (Infographics)

Below is the top 8 difference between C++ using vs typedef.

C++ using vs typedef

C++ using vs typedef Comparison Table

Below given is the comparison table depicting the point to point differences between the ‘using’ and ‘typedef’ in C++:

using typedef
‘using’ in C++ also called alias-declaration can be templatized which can be used to create generic templates. These templates can be modified according to the specific requirements in the future. ‘typedef’ in C++ cannot be templatized unlike ‘using’
With the ‘using’ in C++, it is easier to modify the code in the future without hampering the code at a higher level With the use of ‘typedef’ in C++, it is comparatively difficult to modify the code at the later stages.
Creating a generic type alias is easy similar to one given below:

template<typename T>
using Details = std::unordered_map<Stud_id, std::vector<T>>;

Creating a generic alias using ‘typedef’ on needs to wrap it in a struct similar to one given below in example:

template<typename T>
struct Details {
typedef std::map<Stud_id, std::vector<T>> type;
};
Details<Student_Detail>::type Student_Details;

‘Using’ statement in C++ is not an init-statement so is not used where initialization statements are allowed. In C++, ‘typedef’ is an init-statement so it is used where the initialization statements are allowed.
One of the advantages of ‘using’ statements in C++ is that declaring the function pointer is very clear with that as a comparison to the typedef. Declaring a function pointer using ‘typedef’ is not that much clear like ‘using’.
Creating the alias- templates with the help of ‘using’ statement in C++ does not need any typename before it. ‘typedef’ requires the typename in front of its declaration as wrapped instruct.
Creating aliases with ‘using’ statements helps to develop clean, understandable, and readable code. Although creating an alias using ‘typedef’ is not difficult but it is not that much cleaner and readable as per the programmer’s perspective.
Syntax for declaration with ‘using’ statement:

template<[template-parameters (T1, T2…)]> using [alias] = [original-type];

Syntax for declaration with ‘typedef’ statement:

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (5,233 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

typedef struct struct_type short_type_t;

Key Differences between C++ using vs typedef

Some of the key differences between the ‘using’ statement and ‘typedef’ statement in C++ are as follows:

  • One of the major differences between the using statement and typedef statement in C++ is that ‘using’ can perform all the tasks that ‘typedef’ can and also the one that typedef cannot like ‘using’ can also work with templates.
  • Working with ‘using’ statements is very easy and clear from the programmer’s point of view especially when working with function pointers and its alias definition. In fact ‘using’ statement provides more readability to the code as given in the below example:

typedef void(*func_pointer)(int);
vs
using func_pointer = void(*)(int);

  • Alias declaration which is done by both the ‘using’ and ‘typedef’ statements can be done anywhere in the code like in the class, namespaces and inside the blocks, whereas the template declaration which is done only by the ‘using’ statement cannot be declared inside the class and some strict rules needs to be followed for that.
  • For the generic alias, in case of ‘typedef’ , declaration needs to be wrapped in the struct unlike ‘using’ statement which works very easy and clear and does not require wrapping around it. Let us understand this with the help of an example given below:

template<typename T>
using Accounts = std::unordered_map<Student_ID, std::vector<T>>;
Vs
template<typename T>
struct Accounts {
typedef std::map<Student_ID, std::vector<T>> type;
};
//Using the above like:
Accounts<StudentAccount>::type StudentDetailsAccounts;

  • Though the ‘typedef’ allows the declaring of all the types like function pointers, array pointers, etc, it is a bit lengthy and tricky process using it as compared to the ‘using’ statement in C++ which uses only one line code for that. One can also declare the typedef name to the pointer or structure but before defining structure till the definition and declaration has the same visibility.
  • Re- declarations are possible in case of a typedef declaration. One can declare the same variable using typedef in 2 or more different files and no error will be thrown till both refer to the same type.
  • In C++, ‘typedef’ allows the programmer to declare multiple types at once, unlike the ‘using’ statement.

For example:

typedef int X, *ptr, (*Func)();

Conclusion

The above description clearly explains what is ‘using’ and ‘typedef’ statements in C++ and the major difference between the two. Though both are used for creating the type-alias the major limitation of ‘typedef’ is that it does not work with templates. When it comes to work on non-templates, both ‘using’ and ‘typedef’ perform mechanically the same. So it is the personal choice of the programmer what to use in that case.

Recommended Article

This has been a guide to the top differences between C++ using vs typedef. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles to learn more –

  1. C++ vs Visual C++
  2. C++ vs Objective C
  3. C++ vs C#
  4. C++ vs Java

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ getline()
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ pair
    • C++ this
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
    • C++ QuickSort
    • Sort string C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • C++ Expression
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training Course Learn More