functor

Can two functors be compared for equality?

二次信任 提交于 2019-12-05 07:29:58
Is there a way for a method, which receives two functors as arguments, to find out if they are pointing to the same function? Specifically, having a struct like this: struct FSMAction { void action1() const { std::cout << "Action1 called." << std::endl; } void action2() const { std::cout << "Action2 called." << std::endl; } void action3() const { std::cout << "Action3 called." << std::endl; } private: // Maybe some object-specific stuff. }; And a method like this: bool actionsEqual( const std::function<void(const FSMAction&)>& action1, const std::function<void(const FSMAction&)>& action2) { //

Is There a Indirection Functor?

无人久伴 提交于 2019-12-05 05:47:50
I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of the lambda. Does such a thing exist, or do I need to just use the lambda? Assuming that by "functor" you

Detecting function object (functor) and lambda traits

北慕城南 提交于 2019-12-05 03:38:06
How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)? Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open to supplementing or replacing them. I could do something like this: namespace nsDetail { class Dummy { Dummy(); }; } template<class Fn> struct FnTraits; template<class R> struct FnTraits<R(*)()> { typedef nsDetail::Dummy ParamType; typedef R ReturnType; typedef R Signature(); }; template<class R, class P> struct FnTraits<R(*)(P)> { typedef P

How to call the __invoke method of a member variable inside a class

断了今生、忘了曾经 提交于 2019-12-05 01:47:28
PHP 5.4.5, here. I'm trying to invoke an object which is stored as a member of some other object. Like this (very roughly) class A { function __invoke () { ... } } class B { private a = new A(); ... $this->a(); <-- runtime error here } This produces a runtime error, of course, because there's no method called a. But if I write the call like this: ($this->a)(); then I get a syntax error. Of course, I can write $this->a->__invoke(); but that seems intolerably ugly, and rather undermines the point of functors. I was just wondering if there is a better (or official) way. There's three ways:

Is it possible to create function-local closures pre-C++11?

烂漫一生 提交于 2019-12-05 01:45:29
问题 With C++11, we get lambdas, and the possibility to create functions/functors/closures on-the-fly where we actually need them, not somewhere where they don't really belong. In C++98/03, a nice way to make function-local functors/closures would've been the following: struct{ void operator()(int& item){ ++item; } }foo_functor; some_templated_func(some_args, foo_functor); Sadly, you can't use local types for templates (Visual Studio allows this with language extensions enabled). My train of

Functors in Ocaml

不想你离开。 提交于 2019-12-05 01:35:43
I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last module on this code sample, IntSet. The next line fails, when I try to insert an integer. I get the following type error: Error: This expression has type int but is here used with type SetInt.elt = Set(OrdInt).elt Don't get me wrong, the type system is correct here

Detailed difference between functor's call and function call?

依然范特西╮ 提交于 2019-12-04 23:49:00
The key reason this works is that for_each () doesn’t actually assume its third argument to be a function. It simply assumes that its third argument is something that can be called with an appropriate argument. A suitably defined object serves as well as – and often better than – a function. For example, it is easier to inline the application operator of a class than to inline a function passed as a pointer to function. Consequently, function objects often execute faster than do ordinary functions. An object of a class with an application operator (§11.9) is called a functionlike object, a

Parameter to use std::greater or std::less as argument

南楼画角 提交于 2019-12-04 23:46:28
I would like to make a function with a parameter that accepts either std::greater<int> or std::less<int> as the argument. I'm stuck on the syntax for the parameter, though. This is the format I tried: myFunction(int a, int b, bool *comp(int, int)) { … } … std::greater<int> bigger; myFunction(2, 3, bigger); That doesn't work, though, and I suspect the third parameter is just completely wrong. What should it actually be? cannot convert std::greater<int> to bool* (*)(int, int) user1942027 Functions taking a comparator are usually implemented via templates: template <typename Comparator>

Functors and Applicatives for types of kind (* -> *) -> *

安稳与你 提交于 2019-12-04 22:28:50
I ran into a situation where my code would benefit from using Functor and Applicative -like abstractions, but for types of kind (* -> *) -> * . Defining a higher-kinded functor can be done with RankNTypes like this class HFunctor f where hfmap :: (forall x. a x -> b x) -> f a -> f b But the higher kind version of Applicative is a bit trickier. This is the best I could come up with: class HFunctor f => HApplicative f where hpure :: (forall x. a x) -> f a (<**>) :: f (a :-> b) -> f a -> f b newtype (:->) a b x = HFunc (a x -> b x) infixr 5 :-> We need the :-> wrapper type in order to have

can't initialize functor objects when passing derived class in C++

我的未来我决定 提交于 2019-12-04 16:03:44
This question stems from a previous question I asked here . I cannot use any external libraries or the C++ 11 spec. Meaning I can't use std::bind, std::function, boost::bind,boost::function etc. I have to write it myself. The issue is the following: Consider the code: EDIT Here is a complete program that exhibits the problem as requested: #include <map> #include <iostream> class Command { public: virtual void executeCommand() = 0; }; class Functor { public: virtual Command * operator()()=0; }; template <class T> class Function : public Functor { private: Command * (T::*fptr); T* obj; public: