0

I'm revising C++ after a while, so please bear my curiosity :). Let's suppose we have a function that uses a template as follows:

template<typename T>
void print_array(T numbers[], T n)
{
   //some code
}

What's the point of trying to define more template parameters, i.e.

template<typename T, typename Z> //2nd param Z
void print_array(T numbers[], Z n)
{
   //some code
}

If it yields the same exact results/behavior?

Also from my understanding of templates, is that they were introduced to basically allow you to use a function for different inputs regardless of the parameter data types or even the function return type itself. As a matter of fact, even T print_array(T numbers[], T n) works, albeit there was a warning regarding no return.

So what is even the point of function overloading then if templates are basically a way to handle various parameters and types regardless how the function is called?

halfer
  • 19,824
  • 17
  • 99
  • 186
Cataster
  • 3,081
  • 5
  • 32
  • 79
  • They don't yield the same result. For the first you expect only one type to be provided (`T`), for the other you expect two, potentially different, types. – Ted Klein Bergman Feb 27 '22 at 00:22
  • Because it's not the same behavior. What you first wrote is *very* specific. Write just you first version and try executing it with a `double arr[5];` and a size of 5. I.e. `print_array(arr, 5);` and see what happens. Unrelated, neither of these should emit a warning about a missing return, so if you're getting that the code you posted is not the code delivering that warning. – WhozCraig Feb 27 '22 at 00:22
  • @WhozCraig oh i see why they yielded same results, because my array is int, and so is the 2nd param. `int numbers[SIZE];`. but if array is double then T will be determined as double, and so the second param has to be double as well... – Cataster Feb 27 '22 at 00:26
  • @WhozCraig this is the warning I got: `main.cpp:51:30: required from here main.cpp:26:1: warning: no return statement in function returning non-void [-Wreturn-type]` – Cataster Feb 27 '22 at 00:28
  • bruh why the post got flagged for closure ... come on, so we cant be curious anymore? – Cataster Feb 27 '22 at 00:30
  • That warning is likely for your `main`, not these functions. Neither of these have non-void return types, so they can't possibly trigger that. – WhozCraig Feb 27 '22 at 00:30
  • @WhozCraig right but this warning only shows up if i change function type from void -> T – Cataster Feb 27 '22 at 00:31
  • Well yeah, then your return type is no longer `void`. Again, that's *not* the code you posted here. – WhozCraig Feb 27 '22 at 00:32
  • @WhozCraig gotcha thanks. and what about the function overloading statement. Im curious what is the point of function overloading now given that templates exists and thus we can use the same function name for different parameters in the same fashion – Cataster Feb 27 '22 at 00:44
  • Overloading is not the same. Many Q&As on the subject on this site, [Here's just one of many](https://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading). – WhozCraig Feb 27 '22 at 00:52
  • 1
    In this example, since `n` is clearly meant to represent the number of elements in the array, there is no need to template it. Just use `int` or better `size_t` instead, eg: `template void print_array(T arr[], size_t n)` – Remy Lebeau Feb 27 '22 at 01:00
  • @RemyLebeau thats the first thing I thought may work, however, that means the size of the array MUST be constant, since size_t or int n would be considered a nontype – Cataster Feb 27 '22 at 01:17
  • @Cataster "*that means the size of the array MUST be constant*" - not true. It will work just fine with fixed arrays and dynamic arrays alike, eg: `double arr[5];... print_array(arr, 5);` ... `int n = 10; int *array = new int[n];... print_array(arr, n);` – Remy Lebeau Feb 27 '22 at 01:25
  • 1
    @Cataster You are thinking of a non-type template parameter, as in `template ...`. `n` in `print_array(T arr[], size_t n)` is just a regular function parameter. You can happily pass a value determined at runtime there, it doesn't have to be a compile-time constant. – Igor Tandetnik Feb 27 '22 at 01:25
  • 1
    If you want a function that can be called with two arguments, and the types of those arguments are completely unrelated, your first approach would be useless. – Peter Feb 27 '22 at 01:57
  • 1
    Template is just that - a template. Template functions do not exist until you instantiate them by providing all template parameters they need. This means that template functions can only be used when you have the whole template definition (the function body). Try writing a template function with declaration in header and sources in source file. Try using it in separate source file. It won't work this way because you try to use a function without a definition and in place of template definition you do not know how it will be used. – Maciej Załucki Feb 27 '22 at 02:19
  • E.g. look at std::map template type. It maps keys of one type to values of another type. You want to specify two different types as template parameters. – Sebastian Mar 13 '22 at 19:16

0 Answers0