class psi: public std::pair<std::string ,int>
{
public:
psi(std::string s,int i):std::pair<std::string,int>(s,i)
{
}
};
//Only compare on the first element
booloperator==(const psi& l, const psi& r) {
/*std::cout <<l.first << " : " << r.first;
if(l.first == r.first)
std::cout << ", True"<<std::endl;*/
return l.first == r.first;
}
However, I don't think only typedef causing the problem.
I tried to use full scope reference rather than typedef, but it's still not working. as below:
Yes, I agree that the "typedef" in itself is not the problem. The problem is template ADL
(google "koenig lookup" and read the Wikipedia article). That's why you have to put
the compare function in the std namespace.
But that's kind of icky, and typedefs used this way aren't great either, so that's why
I suggested the strong type. (But I would have it contain a std::pair rather than
inherit).
Most of the types in the STL (pair included) are not set up for inheritance by virtue of the fact that they don't
have virtual destructors, meaning that one cannot delete an object of the derived type through a base class
pointer without causing potential memory leaks because the derived destructor will not run. To me,
inheritance implies that I want my program to hold pointers to (references to) the base for reasons of
polymorphism. But, this is dangerous for the reason I just gave.
I'm going to guess that in your program, you aren't going to upcast pointers to psi's to pointers to
std::pair<std::string, int>. Therefore I would go with containment.
I can also refer you to C++ Coding Standards by Sutter and Alexandrescu (ISBN 0-321-11358-6).
Item #34 is "Prefer composition to inheritance". If you don't have access to the book, here's an excerpt
from the Summary section:
"... Inheritance is the second-tightest coupling relationship in C++, second only to friendship. Tight coupling
is undesirable and should be avoided ... prefer composition to inheritance unless you know that the latter
truly benefits your design."