Wednesday, July 6, 2022

[FIXED] Why does the std::size on an array passed by value do not work?

Issue

Why does the std::size() do not work on a statically allocated array passed by value?

void print_elemTab(int tab[])
{
   // ...
   int size = std::size(tab); //error 
   // ...
}

void test_tab()
{
   const int    TAB_SIZE = 5;
   int          tab[TAB_SIZE] = {};
   // ...
   cout << std::size(tab) << std::endl; //print 5
   print_elemTab(tab);
   // ...
}

I am printing the size and then I am passing the tab in the sub-function print_elemTab() where I am using std::size() again.

I get no matching function error, so I wonder why std::size() works the first time in test_tab() and not in print_elemTab()

Do I have to pass it by reference? And so how do I make it but for an array of any length?

Or do I have to make it in another way because of something that I am not aware of?


Solution

Array designators used in expressions are implicitly converted (with rare exceptions as for example using them in the sizeof operator) to pointers to their first elements.

So in this call

print_elemTab(tab);

the argument expression has the type int *.

On the other hand, a function parameter having an array type is adjusted by the compiler to pointer to the element type of the array.

So for example these function declarations

void     print_elemTab(int tab[]);
void     print_elemTab(int tab[5]);
void     print_elemTab(int tab[100]);

declare the same one function and are equivalent to the following declaration

void     print_elemTab(int *tab);

You nay even include all these declarations in your program though the compiler can issue a message that there are redundant declarations.

Hence within the function you are dealing with a pointer of the type int *. And sizeof( int * ) is usually equal to 4 or 8 depending on the used system.

If you have such a declaration you should change it specifying a second parameter that will keep the number of elements in the passed array like

void     print_elemTab(int *tab, size_t n );

And the function can be called like

print_elemTab(tab, std::size( tab ) );

Another approach is to pass the array by reference. In this case you should declare a template function for example like

template <size_t N>
void     print_elemTab( int ( &tab )[N] );

Within the function you can either directly use the template parameter N as the number of elements in the array. Or you can apply the same standard C++ function std::size to the array.

Or the function can be even more general declared with a second template type parameter like

template <typename T, size_t N>
void     print_elemTab( T ( &tab )[N] );

Another approach is to declare the function like

template <typename Container>
void     print_elemTab( Container &container );

In this case you also may apply the standard function std::size to the parameter container.



Answered By - Vlad from Moscow
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.