- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Type Inference in C++ (auto and decltype)
In this tutorial, we will be discussing a program to understand Type interference in C++ (auto and decltype).
In case of auto keyword, the type of the variable is defined from the type of its initializer. Further with decltype, it lets you extract the type of variable from the called element.
auto type
Example
#include <bits/stdc++.h> using namespace std; int main(){ auto x = 4; auto y = 3.37; auto ptr = &x; cout << typeid(x).name() << endl << typeid(y).name() << endl << typeid(ptr).name() << endl; return 0; }
Output
i d Pi
decl type
Example
#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; }
Output
i c
Advertisements