I have just started programming in C++ and I was playing around with templates.
The following code fails at run time and I have no clue why. (Please note that it is just a sample code and not used in production)
bool maxCompare(string* s1,string* s2){
cout<<*s1<<endl;
cout<<*s2<<endl;
return true; //If I comment this line and instead use the line below, it works fine.
//return *s1 < *s2;
}
int main()
{
string* s1=new string("Hello");
string* s2=new string("Hi");
string *s3= max(s1,s2,maxCompare);
}
Your operator is invalid. In stl’s implementation of max there is an assertion for the comparison function that it can never happen that both
a < bandb < aare true. If you come to think a bit this should never really happen. Your second function does not have this problem so it works. Also check that if you return false it will work.