CKTIS / C++
auto — variable type deduction
The auto keyword stands for a type deduced by the compiler from an initializer. It shortens declarations without losing static typing; writing auto& or const auto& specifies whether the declaration creates a reference.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
auto name = expression;
auto& reference = object;
const auto& readOnly = object;auto is a placeholder for a type, not a dynamic type. In auto name = expression;, the compiler deduces the type from the initializer. An ordinary variable declared this way requires an initializer, so auto name; is an error. The established type does not change on later assignments.
Plain auto does not preserve the initializer's reference or top-level const qualification. Consequently, copy in the example has type int even though source has type const int&. Use auto& to bind a reference to an object, or const auto& to bind a reference to a const-qualified type. For a class object, you can still assign to a mutable data member through such a reference. Such a reference does not freeze the object: changes made through another route remain visible.
In the example, copy holds an independent value, while alias and view refer to value. Assigning through alias changes value, so the expected output is 7 9 9. The static_assert declarations check the types during compilation.
This explanation targets C++23. The supplied site contains an evolving working draft, not a frozen C++23 publication. The example was verified in an isolated environment; that verification does not cover other environments.
Examples
#include <iostream>
#include <type_traits>
int main() {
int value = 7;
const int& source = value;
auto copy = source;
auto& alias = value;
const auto& view = value;
static_assert(std::is_same_v<decltype(copy), int>);
static_assert(std::is_same_v<decltype(alias), int&>);
static_assert(std::is_same_v<decltype(view), const int&>);
alias = 9;
std::cout << copy << ' ' << value << ' ' << view << '\n';
}
Result
7 9 9
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.