functor

How to allow templated functor work on both member and non-member functions

烈酒焚心 提交于 2019-12-17 17:04:38
问题 I got this logging templated functor template<typename RetType, typename Arg1Type, typename Class> class Logger { public: RetType operator()(Arg1Type s, ...) { if(func != 0 && parser != 0) return (parser->*func)(s); else if(nfunc != 0) return nfunc(s); return RetType(); } Logger& operator=(RetType(*fun)(Arg1Type s, ...)) { func = fun; return *this; } void Bind(Class* pars, RetType(Class::*fun)(Arg1Type s,...)) { parser = pars; func = fun; nfunc = 0; } void Bind(RetType(*fun)(Arg1Type s,...))

When to use functors over lambdas

牧云@^-^@ 提交于 2019-12-17 16:12:08
问题 Is there ever a situation where it makes more sense to create a functor than to use a lambda? I know my question is effectively the reverse of when to use a lambda over a functor, but I can't think of a situation in practice where a functor would be preferred over a lambda. Any thoughts on this? 回答1: A lambda is a functor - just defined with a shorter syntax. The problem is that this syntax is limited. It doesn't always allow you to solve a problem in the most efficient and flexible way - or

Is this property of a functor stronger than a monad?

一个人想着一个人 提交于 2019-12-17 15:34:33
问题 While thinking about how to generalize monads, I came up with the following property of a functor F: inject :: (a -> F b) -> F(a -> b) -- which should be a natural transformation in both a and b. In absence of a better name, I call the functor F bindable if there exists a natural transformation inject shown above. The main question is, whether this property is already known and has a name, and how is it related to other well-known properties of functors (such as, being applicative, monadic,

How do I get the argument types of a function pointer in a variadic template class?

萝らか妹 提交于 2019-12-17 05:51:09
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

How do I get the argument types of a function pointer in a variadic template class?

别来无恙 提交于 2019-12-17 05:51:02
问题 This is a follow up of this problem: Generic functor for functions with any argument list I have this functor class (full code see link above): template<typename... ARGS> class Foo { std::function<void(ARGS...)> m_f; public: Foo( std::function<void(ARGS...)> f ) : m_f(f) {} void operator()(ARGS... args) const { m_f(args...); } }; In operator() I can access the args... easily with a recursive "peeling" function as described here http://www2.research.att.com/~bs/C++0xFAQ.html#variadic-templates

Why use functors over functions?

心已入冬 提交于 2019-12-17 04:14:55
问题 Compare double average = CalculateAverage(values.begin(), values.end()); with double average = std::for_each(values.begin(), values.end(), CalculateAverage()); What are the benefits of using a functor over a function? Isn't the first a lot easier to read (even before the implementation is added)? Assume the functor is defined like this: class CalculateAverage { private: std::size_t num; double sum; public: CalculateAverage() : num (0) , sum (0) { } void operator () (double elem) { num++; sum

demote boost::function to a plain function pointer

試著忘記壹切 提交于 2019-12-17 02:12:29
问题 want to pass boost::bind to a method expecting a plain function pointer (same signature). typedef void TriggerProc_type(Variable*,void*); void InitVariable(TriggerProc_type *proc); boost::function<void (Variable*, void*)> triggerProc ... InitVariable(triggerProc); error C2664: 'InitVariable' : cannot convert parameter 1 from 'boost::function<Signature>' to 'void (__cdecl *)(type *,void *)' I can avoid storing a boost::function and just pass the bound functor directly, but then I get similar

Are Standard Library algorithms allowed to copy predicate arguments?

喜你入骨 提交于 2019-12-13 14:12:04
问题 Suppose we'd like to remove duplicate values from a vector of int s. The usual solution is to sort the vector and erase duplicates with erase-remove idiom. But we need to mantain the order of the elements that will not be removed, so we can't sort. So one might come up with a predicate like this and use with with remove_if algorithm: struct comp { std::set<int> s; comp() : s() {} bool operator()(int i) { return !(s.insert(i)).second; } }; But this will break if predicate object will be copied

reusable condition/expression classes

安稳与你 提交于 2019-12-13 12:07:23
问题 I have needed in several occasions some classes to represent and manipulate conditions (typically in a UI so the user builds a query by combining different condition types and then the code can transform that depending on the underlying system to be queried, for example lucene and a db). I searched all over for a reusable set of classes, I am sure this has to be used in many existing places (all the expression languages for starters) but could not find anything easily usable. I ended up

C++ unordered_map with char* key produces unexpected behavior

試著忘記壹切 提交于 2019-12-13 06:12:54
问题 I attempted to use an unordered_map to hash a char* key to an integer value. After writing custom functors to hash and compare char*, the unordered map appeared to work. However, I eventually noticed that the hash would occasionally return incorrect results. I created a test project to reproduce the error. The code below creates an unordered_map with a char* key and custom functors. It then runs 1000x cycles and records any hash errors that occurred. I am wondering if there is something wrong