C++ decltype Keyword
Example
Create a variable with the same type as an expression:
int x = 5;
double y = 2.5;
decltype(x + y) z = x + y; // z is a double
cout << z;
Definition and Usage
The decltype keyword gets the type of an expression. You can use it to declare a variable with the same type, without writing the type yourself.
In the example, x + y is a double (an int plus a double gives a double), so z is also a double.
auto gets the type from the value you assign, while decltype gets the type from any expression that you write inside the parentheses.
Related Pages
Read more about the auto keyword in our C++ auto Tutorial, and about data types in our C++ Data Types Tutorial.