8

For a university exercise, I have been asked to write a template function "print();", which takes two arguments, 1: an array of a generic type, and 2: an int specifying the size of the array. The function should then print out every item in the array to the console. I am having some trouble with the function arguments. The code I currently have is:

   template <typename Type>
   Type print (Type a, Type b)
    {
        Type items;
        Type array;
        a = array;
        b = items;

        for (int i = 0; i < items; i++) {
        std::cout << std::endl << "The element of the index " << i << " is " << array << std::endl;
        std::cout << std::endl;
    }

and in main():

    print(Array[], 10);

Obviously putting Array as an argument isn't returning a value, so I am not sure what else to do. Any ideas?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
willfo
  • 241
  • 1
  • 2
  • 12

2 Answers2

23

The correct way to write it is

Live On Coliru

#include <iostream>

template <typename T, size_t size> void print(const T (&array)[size])
{
    for(size_t i = 0; i < size; ++i)
        std::cout << array[i] << " ";
}

int main() {
    int arr[] = { 1,2,3,4,99};

    print(arr);
}

Prints

1 2 3 4 99
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you, what exactly would I put in as an argument when I call the function in main? If you could give me a quick example that would be great, I'm new to c++! – willfo Oct 20 '15 at 11:25
  • Needs more range-for. Boost.Range `indexed` if indices are needed ;) – Bartek Banachewicz Oct 20 '15 at 11:26
  • 1
    @willfo added a live sample – sehe Oct 20 '15 at 11:28
  • 1
    Why can't we pass the array by value? – Nathan29006781 Apr 04 '22 at 02:05
  • @Nathan29006781 that's how the language is defined - [it _decays_ to a pointer](https://en.cppreference.com/w/cpp/language/array#:~:text=of%20array%20type-,Array%2Dto%2Dpointer%20decay,-There%20is%20an). That's actually just a C-compatibility relic. So, if you want consistency, you totally can: [use `std::array`](https://godbolt.org/z/hKTe8v78r) although in a scenario *that* simple I'd [consider `initializer_list`](https://godbolt.org/z/o9z8MYfbW) – sehe Apr 04 '22 at 02:17
  • @sehe: `print(arr)` passes the `arr` but how the `size` gets calculated? – Rasmi Ranjan Nayak Feb 15 '23 at 15:29
  • @RasmiRanjanNayak It's deduced into `size_t size` by the compiler during overload resolution – sehe Feb 15 '23 at 21:45
6

If you want to pass the array by reference, you could

template <typename T, size_t SIZE>
void print(const T(&array)[SIZE])
{
    for (size_t i = 0; i < SIZE; i++)
        std::cout << array[i] << " ";
}

and then, e.g.

int x[] = {1, 2, 3};
print(x);

LIVE

Otherwise, you can pass it by pointer, note that the array will decay to pointer, and you have to guarantee the correctness of SIZE being passed.

template <typename T>
void print(const T array[], size_t SIZE)
{
    for(size_t i = 0; i < SIZE; i++)
        std::cout << array[i] << " ";
}

and then, e.g.

int x[] = {1, 2, 3};
print(x, sizeof(x) / sizeof(int));

LIVE

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Note that passing (pointer+size) is a bit of an anti-pattern in C++. GSL defines `array_view` for precisely this reason – sehe Oct 20 '15 at 11:30