auto decltype
时间: 2023-10-22 17:15:46 浏览: 39
The auto keyword in C++11 is used to ask the compiler to infer the data type of a variable from its initializer expression. It is useful when the type of the variable is complex or difficult to specify explicitly.
The decltype keyword is used to determine the data type of an expression at compile time. It is useful when the type of an expression is not known or when it is necessary to declare a variable with the same type as an existing expression.
Together, auto and decltype can be used to declare variables with complex or unknown types, without having to manually specify the type. For example:
```
auto x = 5; // x is an int
auto y = 3.14; // y is a double
auto z = x + y; // z is a double, inferred from the expression x + y
decltype(x + y) w; // w is a double, declared with the same type as the expression x + y
```