c++ template function with predicate support -
i want implement priority queue using template.i tried getting error , want pass custom predicate support less function.
#include <iostream> using namespace std; template <typename t, std::size_t n, typename lessfunction> class myclass { typedef std::size_t size_type; public: void push( const t& t) { // size_type index ;//(some value .. 5) //...// if(lessfunction(m_buffer[index], t)) { /// } } private: t m_buffer[n]; }; struct mycompare { bool operator() (int& x, const int& y) { return abs(x) < abs(y); } }; int main() { myclass<int , 8, mycompare> obj; obj.push(1); return 0; }
i getting error.
/home/sanju/code/circular-buffer/main.cpp:17: error: no matching function call 'mycompare::mycompare(int&, const int&)' if(lessfunction(m_buffer[index], t))
please correct me. , have 1 more question how can template use functor function lessfunction ?
you need create instance of class , use overloaded () operator in push member function.
... lessfunction f ; if (f(m_buffer[index], t)) ...
the operator overloaded function not static member function , hence requires instance of class declared called.
Comments
Post a Comment