I'm trying to understand the template function. The ultimate goal is to pass an entire array to a function. There seem to be many different ways to implement this but they all use the template function. Here's one of the simpler examples I've found...
template<size_t N>
void h(Sample (&arr)[N])
{
size_t count = N; //N is 10, so would be count!
//you can even do this now:
//size_t count = sizeof(arr)/sizeof(arr[0]); it'll return 10!
}
Sample arr[10];
h(arr); //pass : same as before!
I thought template<> was used to create a variable that could be used in place of int, float, char, etc.. what's the point of specifying the type (size_t), what does this do?
The size_t N
template parameter is a deduced integral value based upon the array size passed to the template function. Templates parameters can be
Reference: Template Parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With