A real-life example consists of combining template arguments of a non-type type with the output of a template argument to output the size of an array:
template <typename T, unsigned int N> void print_array(T const (&arr)[N]) // both T and N are deduced { std::cout << "["; for (unsigned int i = 0; i != N; ++i) { if (i != 0) { std::cout << ", "; std::cout << arr[i]; } std::cout << "]"; } int main() { double x[] = { 1.5, -7.125, 0, std::sin(0.5) }; print_array(x); }
source share