Naming things with types — and letting the compiler deduce the type for you.
using namespace std::string_literals; // for the ""s suffix
int x = 0;
std::map<std::string, int> m;
auto i = 42; // int
auto d = 3.14; // double
auto s = "hi"s; // std::string
auto& r = x; // reference
const auto& cr = x; // const reference (no copy)
auto* p = &x; // pointer
for (const auto& [k, v] : m) {} // with structured bindings
auto lambda = [](int x){ return x*x; }; // lambdas have no spellable type — auto is requiredauto strips top-level const and references unless you write them:
auto x = cref; copies (drops const/ref); auto& x = cref; keeps them.
int x = 0;
decltype(x) a; // int
decltype((x)) b = x; // int& — extra parens → the expression is an lvalue
decltype(auto) f(); // deduce return type *preserving* ref/const (vs plain auto)decltype(auto) is the tool for perfect-forwarding return types (keeps
references that auto would strip).
auto add(int a, int b) -> int; // trailing return type
auto multiply(int a, int b) { return a*b; } // return type deduced (C++14)
template<class T, class U>
auto sum(T t, U u) -> decltype(t + u); // return type depends on paramsDecompose a tuple/pair/array/aggregate into named pieces:
struct Point { int x, y; };
Point pt{1, 2};
std::pair<int, std::string> p{1, "one"};
auto [id, name] = p; // id=1, name="one"
auto& [a, b] = pt; // bind by reference to mutate
auto [x, y, z] = std::array{1, 2, 3};A structured binding can also be the condition of an if, while or switch
(C++26); see selection statements.
A variable named _ says "I don't need this". Unlike any other name, _ can be
declared again in the same scope, and an unused one draws no warning:
auto [x, _] = std::pair{1, 2.0}; // ignore the second element
auto [y, _] = std::pair{3, 4.0}; // a second `_` in the same scope: fine
static std::mutex m;
std::lock_guard _{m}; // keep a guard alive without naming itOnce a scope holds two _s, using _ is ambiguous:
int _ = 1;
int _ = 2;
int z = _; // ERROR: which `_`?...name binds any number of elements (zero or more) to a pack you can expand.
It's allowed only inside a template (a function template or generic lambda), and
needs GCC 16 or Clang 22; GCC 15 rejects it.
template <class Tuple>
constexpr auto sum(const Tuple& t) {
auto [...xs] = t; // every element
return (xs + ... + 0);
}
static_assert(sum(std::tuple{1, 2, 3}) == 6);
constexpr auto head_and_count = [](auto t) {
auto [head, ...rest] = t; // the first, then the rest
return head * 100 + sizeof...(rest);
};
static_assert(head_and_count(std::tuple{1, 2, 3}) == 102);The bindings become constants. A constexpr auto& binding must refer to an
object with static storage. GCC 16 and Clang 22 have it; GCC 15 doesn't.
P2686 has no feature-test macro, and __cpp_structured_bindings == 202411L (the
packs above) doesn't imply it: Clang 21 and Apple Clang 21 define that value but
reject constexpr auto [x, y] ("decomposition declaration cannot be declared
'constexpr'"), per the platform runs.
struct Point { int x, y; };
constexpr Point origin{3, 4};
constexpr auto [ox, oy] = origin;
static_assert(ox + oy == 7);
constexpr std::array table{1, 2, 3};
constexpr auto& [t0, t1, t2] = table; // refers to a static object
static_assert(t2 == 3);autodrops references and top-level const.auto x = vec[0];copies; useauto&/const auto&to bind without copying.auto&&is a forwarding reference in deduced context (binds to lvalue or rvalue) — not the same as a plain rvalue ref. See value categories.decltype(x)vsdecltype((x))differ: the parenthesized form yields a reference type because(x)is an lvalue expression. Easy to trip over.- Don't overuse
autowhere the type aids readability —auto total = 0;hides that it's anint(and can cause integer/float surprises). It shines for iterators, lambdas, and verbose template types. - A structured binding pack works only inside a template. At namespace
scope or in a plain function,
auto [...xs] = t;is an error. _is only special when it's declared again. A single_is an ordinary variable you can read; after a second_in the same scope, naming it is an error.- Almost-Always-Auto vs clarity is a style call; be consistent with the codebase.