Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to c++ but have some experience in other langues like Java. I saw one example at link[link] where they create a priority queue who have these parameters:


C#
template < class T, class Container = vector<T>,
           class Compare = less<typename Container::value_type> >
class priority_queue;


They say:
"Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container."

So long I still understand, but when they later create a comparator they don't inherit the class "Compare" so how does c++ know that the operator exist? Is this some special rule with some operators?


Here is the code I am talking about:
(I removed some code to make it more clear)
(source link[link])

C
class mycomparison // not something like : Compare  ???
{
public:
  bool operator() (const int& lhs, const int&rhs) const
  {
    return (lhs > rhs);
  }
};

int main ()
{
  int myints[]= {10,60,50,20};

  // using mycomparison:
  priority_queue< int, vector<int>, mycomparison > fourth;
  
  return 0;
}


Thanks in advance, WaZoX.
Posted

The problem you're having seems to be that you're used to run time binding of member functions to objects. The actual member function called is determined by the class of object at runtime. With templates you can choose the function to be called based on the class of the object at compile time. The compiler knows the operator exists because when it generates code for the template class it checks the operator exists on objects of the class it's given.

Provided the comparison class has the semantics member functions the priority_queue is expecting then you don't need to derive anything. Provided whatever is supplied to the priority queue can have function call syntax applied to it you can use it when you create objects that look a bit like priority queues. It's a form of compile time duck typing - provided the comparison "thing" you specify can have the operations the class you're creating needs the compiler can create the code for it.

In fact with that template you don't have to use a class. You can also provide a function name.

Hope this helps, if not then ask away!

Cheers,

Ash
 
Share this answer
 
Comments
WaZoX 23-Sep-10 13:09pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
WaZoX 23-Sep-10 13:15pm    
Thanks a lot, this made things more clear =)
In other formulation, Compare is not a "class" (aggregate of data and functions related to them) but a "template parameter". It is something that at compile time is replaced with the parameter you specify (mycomparison, in your case).

So their internal code that in source may looks like this:
...
Comapre comp;
if(comp(a,b))
{ ... }
...


after the template instantiation becomes like this:
...
mycomparison comp;
if(comp(a,b))
{ ... }
...


that is perfectly translatable into machine code, since all the expression have a definition.
 
Share this answer
 
Comments
WaZoX 23-Sep-10 13:09pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
WaZoX 23-Sep-10 13:11pm    
Thank you very much, I think I understand now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900