In this article, we will explore the differences between typeid and typeof in C++ and guide you on when to use these methods in your code.
What is typeid in C++
The typeid is a C++ operation for retrieving an object’s dynamic type or runtime information. Objects can be variable types, object types, or expression types. To utilize a typeid operator in the program, include the header file <typeinfo>. It is commonly used to determine the type of objects whose true type cannot be determined through static analysis.
Syntax: The following is a basic syntax for utilizing typeid in a C++ program:
or
typeid(expression);
Parameter: The typeid operator accepts a parameter based on the program syntax:
- type: This parameter is given when a runtime type of a variable or object is required. There is no evaluation required within type in this case, and only the type information is required.
- expression: When the runtime type of an expression is required, this parameter is passed. The expression is initially evaluated in this case. The final result’s type information is then provided.
Return value: It returns an object’s runtime or dynamic type information as a type const type_info. If a type is the reference type, then it returns the reference type_info object.
Example
Following is the sample code for the C++ typeid operator:
#include <iostream>
int main() {
using namespace std;
int int_num;
float float_num;
char ch;
cout<<"Type of int_num is: "<< typeid(int_num).name() << endl;
cout<<"Type of float_num is: "<< typeid(float_num).name() << endl;
cout<<"Type of char is: "<< typeid(ch).name() << endl;
return 0;
}
In the preceding code, we declared the variable using predetermined data types, such as int, float, or char. Then we use the typeid() operator to determine the datatypes of the defined variables.
Output
What is typeof in C++
The typeof is a compiler-specific extension in C++ that is typically available in the GNU Compiler Collection (GCC). It returns the type of an expression during compilation, which can be useful for declaring temporary variables in macros that can be used with multiple types.
Meanwhile, it is worth noticing that typeof is not a standard C++ keyword or operator. Instead, in modern C++, the decltype keyword provides similar functionality to typeof and is part of the standard language specification.
Syntax: The following is a basic syntax for utilizing typeof in a C++ program:
Parameter: The typeof/decltype operator accepts a parameter based on the program syntax:
- expression: Expression whose type is need to be determined.
- variable: It is the name of the variable we want to declare with the same type as the expression.
Example
Following is the sample code which demonstrates typeof:
#include <typeinfo>
int main()
{
int num = 0;
typeof(num) num1 = num * 5;
std::cout << "The decltype of num1 is: " << typeid(num1).name() << std::endl;
return 0;
}
The above program defines an integer-type variable named num. We perform the multiplication operation on the num and find the datatype of the applied expression using the typeof.
The same program can also be written using C++ decltype operator.
#include <typeinfo>
int main()
{
int num = 0;
decltype(num) num1 = num * 5;
std::cout << "The decltype of num1 is: " << typeid(num1).name() << std::endl;
return 0;
}
Output
Conclusion
The typeid is a runtime operator that provides the datatype of the variables at runtime while typeof is a compile-time operator that provides the datatype of the variables at the time of compilation. In this guide, we have made useful discussions about typeid() and typeof() with suitable examples.