Function specialization

I have tried everything and can not get this to work.

#include "stdafx.h"

template <class T>
T max5(T *max_val, int elem_num);

//template <> char * max5(char string_arr[][10], int elem_num);

char * testing_func(char string_arr[][10], int elem_num);

void chapter08_exercise05(void)
{
int int_max, arr_size;
int int_arr[6] = {4,7,2,7,9,4};
double double_max;
double double_arr[4] = {3.6, 2.9, 9.4, 6.3};
char string_arr[4][10] = {"Hello", "Goodbye", "Zero", "Calibrate"};
char *string_max;

arr_size = sizeof int_arr / sizeof int_arr[0];
int_max = max5(int_arr, arr_size);

arr_size = sizeof double_arr / sizeof double_arr[0];
double_max = max5(double_arr, arr_size);

arr_size = sizeof string_arr / sizeof string_arr[0];
string_max = testing_func(string_arr, arr_size);

cout << "Maximum integer value: " << int_max << endl;
cout << "Maximum double value: " << double_max << endl;
cout << "Longest string value: " << string_max << endl;
}

template <class T>
T max5(T *max_val, int elem_num)
{
int i, arr_num = 0;

for(i=1; i<elem_num; i++)
if(max_val[i] > max_val[arr_num])
arr_num = i;

return max_val[arr_num];
}

char * testing_func(char string_arr[][10], int elem_num)
{
int i, j = 0;

for(i=0; i<elem_num; i++)
if(strlen(string_arr[i]) > strlen(string_arr[j]))
j = i;

return string_arr[j];
}

/*
template <> char * max5(char string_arr[][10], int elem_num)
{
int i, j = 0;

for(i=0; i<elem_num; i++)
if(strlen(string_arr[i]) > strlen(string_arr[j]))
j = i;

return string_arr[j];
}
*/

Here is the program. I want the function specialization to be doing what testing_func is currently doing in this program. I am aware you can have something along the lines of <char *> after max5 in the function specialization but nothing I've tried in it gets things working.
Oh, you want to specialize the template for arrays, I think.

1
2
3
4
5
6
template< typename T, size_t N >
T max5( T (const &array)[ N ], size_t elem_num )
{
    assert( elem_num < N );
    // ...
}


is what you want.

EDIT: ie, it's not a specialization, it's an overload.
Last edited on
No, I know it could be overloaded but the exercise specifically says it must be a function specialization.
Surely it's: template <> char* max5(char**, int);
I believe you have to specify all but the left most dimension of arrays. (Though I know you can use different notation instead of array[][size_of_second_element] I just find that notation to be clearest.)
Okay help I got from someone else told me to change the

template <> char * max5(char string_arr[][10], int elem_num);

to

template <> char * max5<char *>(char *string_arr[10], int elem_num);

It compiled perfectly with quotes removed, until I tried to utilise the function (change testing_func to max5) at which point it came up with the error

'Failed to specialize function template with the following arguments char[10]'

So still no luck.

Made a foolish error, I forgot to change the array declaration from string_arr[4][10] to *string_arr[4] With that done the code compiles and runs.

The final program:
#include "stdafx.h"

template <class T>
T max5(T *max_val, int elem_num);
template <> char * max5<char *>(char *string_arr[4], int elem_num);

void chapter08_exercise05(void)
{
int int_max, arr_size;
int int_arr[6] = {4,7,2,7,9,4};
double double_max;
double double_arr[4] = {3.6, 2.9, 9.4, 6.3};
char *string_arr[4] = {"Hello", "Calibrate", "Goodbye", "Zero"};
char *string_max;

arr_size = sizeof int_arr / sizeof int_arr[0];
int_max = max5(int_arr, arr_size);

arr_size = sizeof double_arr / sizeof double_arr[0];
double_max = max5(double_arr, arr_size);

arr_size = sizeof string_arr / sizeof string_arr[0];
string_max = max5(string_arr, arr_size);

cout << "Maximum integer value: " << int_max << endl;
cout << "Maximum double value: " << double_max << endl;
cout << "Longest string value: " << string_max << endl;
}

template <class T>
T max5(T *max_val, int elem_num)
{
int i, arr_num = 0;

for(i=1; i<elem_num; i++)
if(max_val[i] > max_val[arr_num])
arr_num = i;

return max_val[arr_num];
}

template <> char * max5<char *>(char *string_arr[4], int elem_num)
{
int i, j = 0;

for(i=0; i<elem_num; i++)
if(strlen(string_arr[i]) > strlen(string_arr[j]))
j = i;

return string_arr[j];
}

Thank you to anyone who took a look.
Last edited on
Topic archived. No new replies allowed.