Type Inference refers to automatic deduction of the data type of an expression in a programming language. Before C++ 11, each data type needed to be explicitly declared at compile-time, limiting the values of an expression at runtime but after the new version of C++, keywords such as auto and decltype are included which allows a programmer to leave the type deduction to the compiler itself.
With type inference capabilities, we can spend less time having to write out things the compiler already knows. As all the types are deduced in the compiler phase only, the time for compilation increases slightly but it does not affect the run time of the program.
1. auto in C++
The auto keyword in C++ specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In the case of functions, if their return type is auto then that will be evaluated by return type expression at runtime. Good use of auto is to avoid long initializations when creating iterators for containers.
Note: The variable declared with auto keyword should be initialized at the time of its declaration only or else there will be a compile-time error.
Example of auto in C++
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
auto x = 4;
auto y = 3.37;
auto z = 3.37f;
auto c = 'a' ;
auto ptr = &x;
auto pptr = &ptr;
cout << typeid (x).name() << endl
<< typeid (y).name() << endl
<< typeid (z).name() << endl
<< typeid (c).name() << endl
<< typeid (ptr).name() << endl
<< typeid (pptr).name() << endl;
return 0;
}
|
Note: We have used typeid for getting the type of the variables.
Here, typeid is an operator that is used where the dynamic type of an object needs to be known.
typeid(x).name() returns the data type of x, for example, it returns:
- ‘i’ for integers, ‘d’ for doubles,
- ‘f’ for float, ‘c’ for char,
- Pi’ for the pointer to the integer,
- ‘Pd’ for the pointer to double,’
- Pf’ for the pointer to float,
- ‘Pc’ for the pointer to char,
- ‘PPi’ for the pointer to pointer to integer.
- Single Pointer results in prefix ‘P’,
- double-pointer results in ‘PP’ as a prefix and so on.
But the actual name returned is mostly compiler-dependent.
Note: Good use of auto is to avoid long initializations when creating iterators for containers.
Example of C++ auto Keyword
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<string> st;
st.insert({ "geeks" , "for" , "geeks" , "org" });
for ( auto it = st.begin(); it != st.end(); it++)
cout << *it << " " ;
return 0;
}
|
Note: auto becomes int type if even an integer reference is assigned to it. To make it reference type, we use auto &.
- Function that returns a ‘reference to int’ type : int& fun() {};
- m will default to int type instead of int& type : auto m = fun();
- n will be of int& type because of use of extra & with auto keyword : auto& n = fun();
2. decltype in C++
The decltype keyword in C++ inspects the declared type of an entity or the type of an expression. ‘auto’ lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.
Explanation of the above keywords and their uses is given below:
Example of decltype in C++
CPP
#include <bits/stdc++.h>
using namespace std;
int fun1() { return 10; }
char fun2() { return 'g' ; }
int main()
{
decltype (fun1()) x;
decltype (fun2()) y;
cout << typeid (x).name() << endl;
cout << typeid (y).name() << endl;
return 0;
}
|
Below is one more example to demonstrate the use of decltype,
Example of C++ decltype Keyword
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 5;
decltype (x) j = x + 5;
cout << typeid (j).name();
return 0;
}
|
Example: C++ Program that Demonstrates the Use of Both auto and decltype
Below is a C++ template function min_type() that returns the minimum of two numbers. The two numbers can be of any integral type. The return type is determined using the type of minimum of two.
CPP
#include <bits/stdc++.h>
using namespace std;
template < class A, class B>
auto findMin(A a, B b) -> decltype (a < b ? a : b)
{
return (a < b) ? a : b;
}
int main()
{
cout << findMin(4, 3.44) << endl;
cout << findMin(5.4, 3) << endl;
return 0;
}
|
decltype vs typeid
Following are some main difference between decltype and typeid
- Decltype gives the type information at compile time while typeid gives at runtime.
- So, if we have a base class reference (or pointer) referring to (or pointing to) a derived class object, the decltype would give type as base class reference (or pointer, but typeid would give the derived type reference (or pointer).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Nov, 2023
Like Article
Save Article