Help with Specialization error
Hey guys, I'm doing a simple program that uses a template function to take an array of any type and returns the largest item in the array, but I also want to use a specialization that takes an array of pointer to char argument and returns the address of the largest string, I'm required to use specialization to make it work.
Here's my code:
Code:
// ex86.cpp -- 6th exercise-8th chapter
#include <iostream>
#include <cstring>
template <typename T>
T maxn(T arr[], int ct);
template <> char* maxn<char>(char* str[], int ct);
int main()
{
using namespace std;
int iarr[6];
cout << "Enter 6 integers:\n";
for (int i = 0; i < 6; i++)
{
cout << "Integer #" << i + 1 << ": ";
cin >> iarr[i];
}
double darr[4];
cout << "Enter 4 doubles:\n";
for (int i = 0; i < 4; i++)
{
cout << "Double #" << i + 1 << ": ";
cin >> darr[i];
}
int max_int;
double max_double;
max_int = maxn(iarr, 6);
max_double = maxn(darr, 4);
cout << "Max integer value = " << max_int << endl;
cout << "Max double value = " << max_double << endl;
char *str[5];
for (int i = 0; i < 5; i++)
str[i] = new char[80];
cout << "Enter 5 strings [max 80 chars]:\n";
for (int i = 0; i < 5; i++)
{
cout << "String #" << i + 1 << ": ";
cin.getline(str[i], 80);
}
char *cp = maxn(str, 5);
cout << "The longest string is: " << cp << endl;
return 0;
}
template <typename T>
T maxn(T arr[], int ct)
{
T max = arr[0];
for (int i = 1; i < ct; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
template <> char* maxn<char>(char* str[], int ct)
{
char* max = str[0];
for (int i = 1; i < ct; i++)
if (strlen(str[1]) > strlen(max))
max = str[i];
return max;
}
Now whenever I compile I get that error in VS2010:
error C2912: explicit specialization; 'char *maxn<char>(char *[],int)' is not a specialization of a function template
I made another instance of the code using non-template function to overload when using char* and the program worked like a charm, here's the code:
Code:
// ex86.cpp -- 6th exercise-8th chapter
#include <iostream>
#include <cstring>
template <typename T>
T maxn(T arr[], int ct);
char* maxn(char* str[], int ct);
int main()
{
using namespace std;
int iarr[6];
cout << "Enter 6 integers:\n";
for (int i = 0; i < 6; i++)
{
cout << "Integer #" << i + 1 << ": ";
cin >> iarr[i];
}
double darr[4];
cout << "Enter 4 doubles:\n";
for (int i = 0; i < 4; i++)
{
cout << "Double #" << i + 1 << ": ";
cin >> darr[i];
}
int max_int;
double max_double;
max_int = maxn(iarr, 6);
max_double = maxn(darr, 4);
cout << "Max integer value = " << max_int << endl;
cout << "Max double value = " << max_double << endl;
cin.get();
char *str[5];
for (int i = 0; i < 5; i++)
str[i] = new char[80];
cout << "Enter 5 strings [max 80 chars]:\n";
for (int i = 0; i < 5; i++)
{
cout << "String #" << i + 1 << ": ";
cin.getline(str[i], 80);
}
char *cp = maxn(str, 5);
cout << "The longest string is: " << cp << endl;
return 0;
}
template <typename T>
T maxn(T arr[], int ct)
{
T max = arr[0];
for (int i = 1; i < ct; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
char* maxn(char* str[], int ct)
{
char* max = str[0];
for (int i = 1; i < ct; i++)
if (strlen(str[1]) > strlen(max))
max = str[i];
return max;
}
However, in the original quiz I'm required to use specialization, can anyone please point to what I did do wrong in the first instance of code?
Thanks all for your help.
Re: Help with Specialization error
you simply forgot the '*':
Quote:
template <> char* maxn<char*>(char* str[], int ct);
Re: Help with Specialization error
Quote:
Originally Posted by
superbonzo
you simply forgot the '*':
I'm just stupid, but shouldn't it be a best match even with out the * ?
Re: Help with Specialization error
Quote:
Originally Posted by
iExtreme
I'm just stupid, but shouldn't it be a best match even with out the * ?
It's not a "best match" because it's not even a legal definition. The signature of a function template specialization must match the signature of the to be specialized function template.
So you can write
Code:
template <> char* maxn<char>(char* str[], int ct); // illegal, it's a potential specialization of a function template with signature template <class T> T* maxn(T* str[],int);
template <> char* maxn<char*>(char* str[], int ct); // ok, it's a specialization of a function template with signature template <class T> T maxn(T str[],int);
template <> char* maxn(char* str[], int ct); // ok, as above; but this time the type is deduced
char* maxn(char* str[], int ct); // ok, a function overload
Re: Help with Specialization error
Quote:
Originally Posted by
superbonzo
It's not a "best match" because it's not even a legal definition. The signature of a function template specialization must match the signature of the to be specialized function template.
So you can write
Code:
template <> char* maxn<char>(char* str[], int ct); // illegal, it's a potential specialization of a function template with signature template <class T> T* maxn(T* str[],int);
template <> char* maxn<char*>(char* str[], int ct); // ok, it's a specialization of a function template with signature template <class T> T maxn(T str[],int);
template <> char* maxn(char* str[], int ct); // ok, as above; but this time the type is deduced
char* maxn(char* str[], int ct); // ok, a function overload
Oh, thanks for the clarification, but I think using the first form is the best? because type deduction isn't standard C++ as far as I know, thank you very much for you help.