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
Post a Comment