decltype
suggest changeYields the type of its operand, which is not evaluated.
- If the operand
eis a name without any additional parentheses, thendecltype(e)is the declared type ofe.int x = 42; std::vector<decltype(x)> v(100, x); // v is a vector<int> - If the operand
eis a class member access without any additional parentheses, thendecltype(e)is the declared type of the member accessed.struct S { int x = 42; }; const S s; decltype(s.x) y; // y has type int, even though s.x is const - In all other cases,
decltype(e)yields both the type and the value category of the expressione, as follows:* If `e` is an lvalue of type `T`, then `decltype(e)` is `T&`. * If `e` is an xvalue of type `T`, then `decltype(e)` is `T&&`. * If `e` is a prvalue of type `T`, then `decltype(e)` is `T`.This includes the case with extraneous parentheses.
int f() { return 42; } int& g() { static int x = 42; return x; } int x = 42; decltype(f()) a = f(); // a has type int decltype(g()) b = g(); // b has type int& decltype((x)) c = x; // c has type int&, since x is an lvalueThe special form
decltype(auto)deduces the type of a variable from its initializer or the return type of a function from thereturnstatements in its definition, using the type deduction rules ofdecltyperather than those ofauto.const int x = 123; auto y = x; // y has type int decltype(auto) z = x; // z has type const int, the declared type of x
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents