CKTIS / C++
decltype — an expression's type without evaluation
decltype specifies a type from an expression without executing it. It distinguishes an object's name from a general expression: decltype(x) can denote int, while decltype((x)) denotes int&.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
decltype(expression)
decltype(auto) name = expression;decltype(expression) is a type specifier; its operand is unevaluated. For an ordinary name or member access without extra parentheses, it gives the named entity's type, including qualifiers and references. Thus, for int x, decltype(x) is int.
Otherwise, value category determines the result: an lvalue gives T&, an xvalue gives T&&, and a prvalue gives T. Extra parentheses matter: decltype((x)) is int&. Structured binding names have a special referenced-type rule; non-type template parameter names use the parameter's type after deduction.
decltype(auto) applies decltype rules to the initializer instead of ordinary auto deduction: auto a = (x); creates a copy, whereas decltype(auto) b = (x); preserves a reference. Preserving a reference does not automatically extend the object's lifetime; avoid returning references to local variables.
The example checks types with static_assert; decltype(++x) does not increment x. This entry targets C++23. The source is an evolving draft, not a frozen C++23 text; later extensions are omitted.
Examples
#include <iostream>
#include <type_traits>
#include <utility>
int main() {
int x = 4;
decltype(x) copy = x;
decltype((x)) alias = x;
using Probe = decltype(++x);
static_assert(std::is_same_v<decltype(copy), int>);
static_assert(std::is_same_v<decltype(alias), int&>);
static_assert(std::is_same_v<Probe, int&>);
static_assert(std::is_same_v<decltype(std::move(x)), int&&>);
static_assert(std::is_same_v<decltype(x + 1), int>);
alias = 9;
std::cout << x << ' ' << copy << '\n';
}
Result
9 4
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.