Type inference is a feature of c++ language. It is done by the c++ compiler.
inference
deduce
deduction
If a variable is declared auto and initialized with a value, the type will be deduced from the initialized value's literal type.
auto x = 234; auto y = 2.5;
x is deduced as int, y is deduced as double commonly.
decltype can deduce a type from a variable.
int x; std::string y; using t1 = decltype(x); using t2 = decltype(y);
t1 is inferenced as int, t2 is inferenced as std::string.
template <typename my_type>
class my_class
{
private:
my_type __my_value;
public:
my_class(const my_type & my_value__):
__my_value{my_value__}
{
}
};
my_class<int> x{2345};
my_class y{9.8872};
The type of x is my_class<int> as specified.
The type of y is my_class<double> as its template argument is deduced.
Template deduction guide is a c++ glossary that stands for how to deduce a template specialized type with a special c++ code block.
#include <iostream>
#include <stdfloat>
namespace my_space
{
template <typename my_type>
class my_class
{
public:
my_class(const my_type &)
{
}
};
// This is template deduction guide
my_class(const int &) -> my_space::my_class<float>;
// This is template deduction guide
template <std::floating_point type_t00>
my_class(const type_t00 &) -> my_space::my_class<std::float128_t>;
}
int main()
{
my_space::my_class x{123};
my_space::my_class y{1.23};
using t1 = decltype(x);
using t2 = decltype(y);
static_assert(std::same_as<t1, my_space::my_class<float>>);
static_assert(std::same_as<t2, my_space::my_class<std::float128_t>>);
}
Because those two template deduction guides,
x's type is deduced as my_space::my_class<float>
and y's type is dedueced as my_space::my_class<std::float128_t>