Hi I am trying to write a generic isLess compare function for int, double, null terminated character array etc. Below is my code for same, please help me understand how could we use this function for null terminated strings too.
#include<iostream>
using namespace std;
template<typename T>
bool isLess(T &x, T &y)
{
return x < y;
}
int main()
{
int a(10), b(20);
double c(2.0), d(3.0);
cout<<isLess<int>(a,b)<<endl;
cout<<isLess<double>(c,d)<<endl;
//For above types, generic comparator works fine
//but if we have to compare character represented string, how can we do this in our comparator
//assume charaters strings are compared the same way as strcmp.
#include<iostream>
usingnamespace std;
template<typename T>
bool isLess(T x, T y)
{
return x < y;
}
template<>bool isLess(char *x, char *y)
{
return (strcmp(x, y) < 0);
}
int main()
{
int a(10), b(20);
double c(2.0), d(3.0);
cout<<isLess<int>(a,b)<<endl;
cout<<isLess<double>(c,d)<<endl;
//For above types, generic comparator works fine
//but if we have to compare character represented string, how can we do this in our comparator
//assume charaters strings are compared the same way as strcmp.
char *e = "str12";
char *f = "str1";
cout<<isLess<char*>(e, f)<<endl;
return 0;
}
Did you forget to #include the <cstring> header file required for the strcmp() function. Also you really don't need the template<> for the C-string function.
You also don't need the "template" syntax on any of the function calls:
@jlib : tnx for your response jlib, I understand that we could use overloaded function, but I wish to clarify this concept hence looking for template specialization.
Even if we add char * at line no. 33, it gives same error.
#include<iostream>
#include<cstring>
usingnamespace std;
template<typename T>
bool isLess(T &x, T &y)
{
return x < y;
}
template<>
bool isLess(char *x, char *y)
{
return (strcmp(x, y) < 0);
}
int main()
{
int a(10), b(20);
double c(2.0), d(3.0);
cout<<isLess<int>(a,b)<<endl;
cout<<isLess<double>(c,d)<<endl;
//For above types, generic comparator works fine
//but if we have to compare character represented string, how can we do this in our comparator
//assume charaters strings are compared the same way as strcmp.
char *e = "str1";
char *f = "str2";
cout<<isLess<>(e,f)<<endl;
return 0;
}
> [Error] template-id 'isLess<>' for 'bool isLess(char*, char*)' does not match any template declaration
Overload resolution only selects a base template (or a nontemplate function, if one is available). Only after it's been decided which base template is going to be selected, and that choice is locked in, will the compiler look around to see if there happens to be a suitable specialization of that template available, and if so that specialization will get used. http://www.gotw.ca/publications/mill17.htm
In the posted code, the base template expects references; the attempted specialisation (which expects values) is not looked at.
@JLBorges : Tnx for having a look. I understand that if we use function overloading then we don't need to use template specialization.
But I am trying to understand how to use template specialization if we are not using funciton overloading.
In the below code, if instead of overloaded function we use template specialization as follows then we get error : [Error] template-id 'isLess<>' for 'bool isLess(const char*&, const char*&)' does not match any template declaration.
I thought that for specialization char * will be considered T and hence we can pass arguments as
"const T& e" (where T = char *) i.e. "const char *& e".
Could you kindly help me with why this is not working.