Problem passing C++ comparator functor as a parameter -


suppose have template class called linkedlist contains method called sort want able have default comparator assuming type t can compared < operator.

however, want able override other types.

this part of custom library i'm extending. no stl.

e.g.

template <class t, class comparator<t> > class linkedlist {   ...   node* mergesort(node* list)   {       ...       if (comparator<t>(nodea,nodeb))       ...   }   ... };   typedef pair<int, int> listentry;  struct sorter {     inline sorter(){}     inline bool operator()(listentry const& a, listentry const& b)     {         return a.second < b.second;     } };  pair<int, int> entry; linkedlist<listentry, sorter()> list;  list.pushfront( listentry(5,9) ); list.pushfront( listentry(77,5) ); list.pushfront( listentry(4,1) ); list.pushfront( listentry(8,44) ); list.pushfront( listentry(1,64) ); list.pushfront( listentry(3,5) ); n = list.mergesort(node* n); 

this class looks like. mergesort private data member i've temporarily made public while fool around , try make work. mergesort called sort.

i haven't been able compile this. wondering how pass functor objects around this.

you cannot template <class t, class comparator<t>>, i.e. have 1 template parameter parameter of parameter. standard library , use default parameters:

template <class t, class comparator = std::less<t>> class myclass {   ... } 

also, comparator going object need instantiate @ point. again, default arguments key:

void myclass::dosomething(foo x, comparator comp = comparator()) {   /* ... */   if (comp(a, b)) { /* ... */ }   /* ... */ } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -