0
#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.

8
  • do you understand how the template gets to know what T is? Commented Jun 23, 2021 at 8:12
  • What is understood is T is referring to the type of the datatype, based on which the std::array is being defined here... But what i have failed to understand is how the size of the array is being stored in the variable size[std::size] Commented Jun 23, 2021 at 8:14
  • std::array myArray5 is actually std::array<double, 5> myArray5... Commented Jun 23, 2021 at 8:14
  • 1
    5 (std::size_t N) is "stored" in std::array in similar way than double (typename T) is. it belongs to the type. Commented Jun 23, 2021 at 8:19
  • 1
    The template function doesn't calculate the size of the array. The compiler creates an instantiation the function that accepts a std::array<T, size> for each specified type T (double in both your examples) and size (5 for myArray5 and 7 for myArray7). Commented Jun 23, 2021 at 8:21

3 Answers 3

3

First, std::array is a template. When you write

std::array myArray5{ 9.0, 7.2, 5.4, 3.6, 1.8 }; 

Then Class Template Argument Deduction (CTAD, deduction guides for std::array can be found here) is applied to infer that myArray5 is of type std::array<double,5>. That is where the size is "calculated".

Then when you call the function template:

printArray(myArray5); 

The template arguments are deduced from the function parameter (see here). myArray5 is std::array<double,5>, hence T == double and size == 5.

Sign up to request clarification or add additional context in comments.

1 Comment

1

When you call a function template, you can provide template arguments explicitly:

printArray< double, 5 >(myArray5); 

If you don't provide them, a compiler tries to deduce them according to the type of the passed function argument. In your case, the passed argument myArray5 has the type std::array<double,5>. Consequently, a compiler deduces T to double and size to 5.

Comments

0

It is not quite clear which aspect of array size you are asking about? Size of the array itself, or the size of the data types resolution.

But with my best guess let me explain both.

C++ is a statically typed language. Which means that the data types of any variable, including templated types are resolved during compilation time.

Right at the point where you are defining the template typed array,

std::array myArray5{ 9.0, 7.2, 5.4, 3.6, 1.8 }; 

At compile-time, C++ sees individual element, say 9.0, based on the default literal-type associates that to double types, and hence forth makes an array of doubles.

Hope that helps you understand how to reason about size of each elements in an array. And the total size is just simple multiplication of number of elements in it.

Now, the I know better what you are asking, the default array constructor is std::array<T, N>, since, we already know how many elements you are passing to array, the corresponding arguments are filled. Where T being datatype, and N being some fixed size data (which you have already provide { 9.0, 7.2, 5.4, 3.6, 1.8 } = 5), which can be resolved in the compile time.

2 Comments

I am referring to the below snippet, where we are calculating the size of array which is passed as an argument. ``` 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'; } ```
@Virenmahajan refresh my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.