
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is type deduction in C++?
Type inference (or type deduction) refers that the compiler can figure out the datatype of a variable or expression automatically, without the programmer needing to write it out.
This feature is available in many strongly typed languages like C++, where the type system is strict but the compiler helps to reduce the typing effort.
Following is the simple example to understand the type deduction in C++.
#include<iostream> using namespace std; int main() { // Let the compiler deduce the type from the value auto number = 50; // int auto pi = 3.14; // double cout<<"number = "<<number<<endl; cout<<"pi = "<<pi<<endl; return 0; }
Following is the output to the above program:
number = 50 pi = 3.14
In C++, we use the type deduction with two keywords: "auto" and "decltype".
Type Deduction using auto Keyword
The auto keyword is used for automatic type deduction. For example, you want to create an iterator to iterate over a vector, you can simply use auto for that purpose.
Syntax of the auto keyword
Following is the syntax to deduce the auto keyword and the compiler deduces the type of variable_name from the value.
auto variable_name = value;
Example
This program uses 'auto' keyword to automatically deduce the type of the vector iterator and prints the first element of the vector using that iterator.
#include<iostream> #include<vector> using namespace std; int main() { vector<int> v = {10, 20, 30, 40}; // Using 'auto' to simplify iterator declaration auto it = v.begin(); // Type deduced as vector<int>::iterator cout<<"First element in the vector: "<<*it<<endl; return 0; }
Following is the output to the above program:
First element in the vector: 10
Type Deduction using decltype Keyword
The decltype keyword is used to get the type of a variable or expression without actually using or changing the value. For example, if you have a variable int x = 5;, then writing decltype(x) y; will declare y as an int, the same type as x.
You can say that "the same deduced type as x" for decltype to clarify that decltype doesn't deduce the value, only the type.
Syntax of the decltype keyword
Following is the syntax to deduce the decltype keyword and the compiler deduces the type of variable_name from the type of the expression, without evaluating it.
decltype(expression) variable_name;
Example
This program uses 'decltype' keyword to create variables with the same type as existing variables or expressions, and prints their values.
#include<iostream> using namespace std; int main() { int a = 5; float b = 6.5; // Use decltype to get the type of a decltype(a) x = 100; // x is int decltype(a + b) result = a + b; // result is float (a + b) cout<<"x = "<<x<<endl; cout<<"result = "<<result<<endl; return 0; }
Following is the output to the above program:
x = 100 result = 11.5