Type inference is the ability of the compiler to deduce the type of an object or expression at compile time.

To create a variable with an inferred type, use the auto keyword:

auto x = 123;      // int
auto y = x + 456;  // int
auto z = "str";    // const char*

auto always needs an initialiser:

auto x;  // ERROR.

auto drops top-level const and references, so write them explicitly to keep them:

const int a = 123;
auto x = a;   // int, const dropped.
auto& y = a;  // const int&

int b = 456;
auto& z = b;  // int&

References