
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
Relation Between auto and decltype in C++
The auto and decltype serve different purposes so they don't map one-to-one. The auto is a keyword in C++11 and later that is used for automatic type deduction. The decltype type specifier yields the type of a specified expression. Unlike auto that deduces types based on values being assigned to the variable, decltype deduces the type from an expression passed to it. The value returned by decltype can directly be used to define another variable.
The auto follows the rules of template parameter deduction. You can read more about these rule at Template Argument Deduction
While decltype has rules it should follow defined in the standard. Here is an excerpt from the standard with these rules: Here.
Similarly to the sizeof operator, the operand of decltype is unevaluated. Informally, the type returned by decltype(e) is deduced as follows:
- If the expression e refers to a variable in local or namespace scope, a static member variable or a function parameter, then the result is that variable's or parameter's declared type
- If e is a function call or an overloaded operator invocation, decltype(e) denotes the declared return type of that function
- Otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; if e is an rvalue, the result is T
Below are the examples to understand the Relation Between auto and decltype in C++.
Example 1: Using auto Keyword
This program creates a vector of integers and uses auto to automatically deduce the iterator type to access the first element, and then it prints that first element.
#include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers = {7, 3, 2, 9, 5}; // 'auto' deduces the type of iterator auto it = numbers.begin(); cout<<"First element: "<<*it<<endl; return 0; }
Following is the output to the above program:
First element: 7
Example 2: Using decltype Keyword
This program uses decltype keyword to create new variables x and y with the same types as a and b, and then it prints their values.
#include<iostream> using namespace std; int main() { int a = 6; float b = 7.28; // 'decltype' gives the type of variable a decltype(a) x = 100; // x is int decltype(b) y = 12.3; // y is float cout<<"x: "<<x<<", y: "<<y<<endl; return 0; }
Following is the output to the above program:
x: 100, y: 12.3
Example 3: Using both auto and decltype Keywords
This program uses 'auto' to store the result of a function and 'decltype' to create another variable of the same type, then prints both values.
#include<iostream> using namespace std; int add(int x, int y) { return x + y; } int main() { int a = 71, b = 29; // Use 'auto' to store the return value auto result = add(a, b); // Use 'decltype' to declare another variable with the same type as result decltype(result) total = result + 10; cout<<"Result: "<<result<<", Total: "<<total<<endl; return 0; }
Following is the output to the above program:
Result: 100, Total: 110