#include <array> #include <cstdef> #include <iostream> // printArray is a template function template <class T, std::size_t size> // parameterize the element type and size void printArray(const std::array<T, size>& myArray) { for (auto element : myArray) std::cout << element << ' '; std::cout << '\n'; } int main() { std::array myArray5{ 9.0, 7.2, 5.4, 3.6, 1.8 }; printArray(myArray5); std::array myArray7{ 9.0, 7.2, 5.4, 3.6, 1.8, 1.2, 0.7 }; printArray(myArray7); return 0; } Can someone please help understand how the size of the array is calculated by the function template.
Tis?std::array myArray5is actuallystd::array<double, 5> myArray5...5(std::size_t N) is "stored" instd::arrayin similar way thandouble(typename T) is. it belongs to the type.std::array<T, size>for each specified typeT(doublein both your examples) andsize(5formyArray5and7formyArray7).