Non-type Template in Array
You can pass size of an array by only using a template parameter. Here is an example code of it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> template <typename T, int size> T sum(T (&parr) [size]) { T sum{}; for (int 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 EXIT_SUCCESS; } |