auto
means "the variable's type is deduced from the initialiser."
decltype
refers to a type in an arbitrary context.
Here's an example where you can't use auto
:
template <typename T, typename U, typename V>
void madd(const T &t, const U &u, const V &v, decltype(t * u + v) &res)
{
res = t * u + v;
}
There is no initialiser in the parameter declaration (and there can't be), so you can't use auto
there.
The thing is, 99% of uses for decltype
is in templates. There's no equivalent functionality for it there. In non-template code, auto
is usually what you want to use.