Creating a template predicate class requiring a pointer to method function, and ensuing compiler errors?

I don't know of any easy way to do this using the bits provided with the STL. There is probably some clever boost way, using iterator adapters, or boost::lambda, but personally I wouldn't go that way Obviously C++0x lambdas will make all this easy Your problem is attempting to cast a function like this: const std::string&(Atom::*)() into a function like this: std::string (Atom::*)() If you replace your typedef R (U::*fn_t)() with typedef const R& (U::*fn_t)() const then it should work The following avoids this problem and also provides type inference so that you can just write mem_fun_eq(&Atom::Name, atomname) It compiles for me, although I haven't tested it template class mem_fun_eq_t : public std::unary_function { private: R (U::*fn_)() const; S val_; public: mem_fun_eq_t(R (U::*fn )() const, S val) : fn_(fn), val_(val){} bool operator()(U * u) { return (u->*fn_)() == val_; } }; template mem_fun_eq_t mem_fun_eq(R (U::*fn)() const, S val) { return mem_fun_eq_t(fn, val); }.

I don't know of any easy way to do this using the bits provided with the STL. There is probably some clever boost way, using iterator adapters, or boost::lambda, but personally I wouldn't go that way. Obviously C++0x lambdas will make all this easy.

Your problem is attempting to cast a function like this: const std::string&(Atom::*)() into a function like this: std::string (Atom::*)() If you replace your typedef R (U::*fn_t)(); with typedef const R& (U::*fn_t)() const; then it should work. The following avoids this problem and also provides type inference so that you can just write mem_fun_eq(&Atom::Name, atomname). It compiles for me, although I haven't tested it.

Template class mem_fun_eq_t : public std::unary_function { private: R (U::*fn_)() const; S val_; public: mem_fun_eq_t(R (U::*fn )() const, S val) : fn_(fn), val_(val){} bool operator()(U * u) { return (u->*fn_)() == val_; } }; template mem_fun_eq_t mem_fun_eq(R (U::*fn)() const, S val) { return mem_fun_eq_t(fn, val); }.

Basically, you call on mem_fun to create an object that accepts two arguments T* and a template argument to the function A if it has one (or void if it doesn't). Hence you combine it like so: template struct MyPredicate{ MyPredicate(MemFunc _functionObj, CompareType _value) : m_Value(_value), m_Function(_functionObj){} bool operator()(const T &_input){ return m_Value == m_Function(_input); } private: MemFunc m_Function; CompareType m_Value; }; Edit: Ok, that's not completely working so why not have: struct NamePred: binary_function{ bool operator()(Atom *_obj, string _val){ return _obj->Name() == _val; }; }; then use bind2nd find_if( atoms.begin(), atoms.end(), bind2nd( NamePred, "yo" ) ).

Yes, just having the type of the function be a template parameter makes most of the pain go away. You should do this. – Richard Wolf Jul 27 '10 at 0:22 Can the MemFunc type be deduced?

– Shamster Jul 27 '10 at 0:24 @Shamster: good point. A wrapper function would be required to achieve the required type deduction, similar to the mem_fun and mem_fun_ref wrapper functions. – Richard Wolf Jul 27 '10 at 0:29 I'm actually trying to get it to compile with VS2008 but that's exactly the problem I'm running into.It's having issues with resolving the mem_fun return type.

Damn, thought this would help. I'll keep working at it. – wheaties Jul 27 '10 at 0:30.

I'm building a series of predicates that duplicate lots of code, and so are being changed into a single template function class based on the std::unary_function. Creates a different error! Firstly, am I reinventing the wheel with this predicate?

Is there something in the STL that I've missed that does the same job in a single class? I can always break the predicate down into several more specific ones, but I'm trying to avoid that. Secondly, can you help me with the compiler errors?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send