C++ typeid

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Introduction

The typeid is an operator in C++ used to know an object's runtime or dynamic type. The typeid operator is in the typeinfo library header in C++. So, to use the typeid operator, we must include the typeinfo library header.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax

OR

Parameters

The C++ typeid operator accepts a single parameter, and this parameter is based on the syntax that is used in the program.

  • Type: The type parameter is passed inside the typeid operator when we require information about the runtime type of a variable or an object. We do not need to evaluate anything or perform any pre-computation in this parameter. We pass the type parameter, and the information about the runtime type is extracted.

  • Expression: The expression parameter is passed inside the typeid operator when we require information about the runtime type of an expression. In this parameter, we need to first evaluate the expression, and then the information about the runtime type is extracted.

Return Value

The C++ typeid operator returns the runtime or dynamic type information of an expression or object. The data type of the returned value is const type_info.

Sharpen Your Fundamentals with Free Learning

What are the Uses of C++ Typeid Operator?

According to the passed operand type, the typeid operator can be used differently, as shown below.

When the Operand is a Variable of the Object

In this code, we will see how we can get information about the type of the parameter when the variable is passed as a parameter.

C++ Implementation

Output

Explanation

  • In the first step, we have declared four variables, i.e., a of int data type, b of char data type, c of float data type, and d of double data type.
  • Then we assigned the type_info of all the variables using the C++ typeid() operator.
  • Then, we checked the types of all the variables and accordingly produced the following output
    • Type of a is i, which means that a is of int data type because i indicates int.
    • Type of b is c, which means that b is of char data type because c indicates char.
    • Type of c is f, which means that c is of float data type because f indicates float.
    • Type of d is d, which means that d is of double data type because d indicates double.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

When the Operand is an Expression

In this code, we will see how we can get information about the type of parameter when the expression is passed as a parameter.

C++ Implementation

Output

Explanation

  • In the first step, we have declared four variables, i.e., a of int data type, b of char data type, c of float data type, and d of double data type, and initialized them with the values of 5, 'x', 1.5, and 3.5095.
  • Then we assign the type_info of some expressions using the C++ typeid() operator.
  • Then, we checked the types of all the variables and accordingly produced the following output
    • **Expression a+b : i, which means that a+b is of int data type because i indicates int.
    • **Expression a * c : f, which means that a * c is of float data type because f indicates float.
    • **Expression d * c : d, which means that d * c is of double data type because d indicates double.
    • **Expression a+d : d, which means that a+d is of double data type because d indicates double.

Examples of C++ typeid Operator

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 1

To get information about the type of simple variables:

C++ Implementation

Output

Example 2

To get information about the type of both polymorphic and non-polymorphic classes:

C++ Implementation

Output

Example 3

Getting information about the type of expression

C++ Implementation

Output

Learn More

Conclusion

In this quick tutorial, we have discussed the typeid() operator in C++. We can extract the following conclusions from the article.

  • The typeid operator is present in the typeinfo library header in C++. So, to use the typeid operator, we must include the typeinfo library header.
  • The typeid operator can be used to get information about the variables and expressions.
  • The C++ typeid operator returns the runtime or dynamic type information of an expression or object.