I have seen this meta function used, but never really understod why and in what context it is required. Can someone explain it with an example?
template <
It introduces a non-deduced context, when deducing template arguments from function parameters. For example, say you have a function with the following signature :
template
void foo(T a, T b);
If someone were to call foo(123L, 123)
, they'd get a substitution error, as T
cannot match long int
and int
at the same time.
If you want to match only the first argument, and convert the other one implicitly if needed, you can use identity
:
template
void foo(T a, typename identity::type b);
Then b
does not participate in type deduction, and foo(123L, 123)
resolves to foo
.