C++ how to pass method as a template argument -


suppose have class x:

class x {     // ...     size_t hash() const { return ...; } }; 

i create std::tr1::unordered_map<x, int, hashfn> want pass in x::hash() hashfn. know can declare own functor object. feel there should way directly passing pointer x::hash().

is there?

no; you've shown it, need small utility struct:

#include <functional>  template<typename t, std::size_t (t::*hashfunc)() const = &t::hash> struct hasher : std::unary_function<t, std::size_t> {     std::size_t operator ()(t const& t) const     {         return (t.*hashfunc)();     } }; 

then can create unordered_map so:

std::tr1::unordered_map<x, int, hasher<x> > m; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -