Non-type Template in Array
Deducing the size of a C array with a non-type template parameter, so you don't have to pass the size as an argument.

You can get the size of an array by only using a template parameter. Here is an example code of it.
#include <cstddef>
#include <iostream>
template <typename T, std::size_t size>
T sum(T (&parr) [size]) {
T sum{};
for (std::size_t i{0}; i < size ; i++){
sum += parr[i];
}
return sum;
}
int main()
{
int arr[]{1,3,2,5,3};
std::cout << sum(arr) << std::endl;
return 0;
}
The array is passed by reference, so it doesn’t decay to a pointer and the compiler can deduce size from its type (int[5]).
Update (2026): The standard library already uses the same trick in
std::size(arr)(C++17). In C++20 you can also take astd::span<const T>, which works with C arrays,std::arrayandstd::vector.