functor

What is the difference between a Functor and the Command pattern?

I am very familiar with the Command pattern, but I don't yet understand the difference in theory between a Functor and a command. In particular, I am thinking of Java implementations. Both are basically programming "verbs" represented as objects. However, in the case of functors, as I have seen from some examples anonymous inner class im...

demote boost::function to a plain function pointer

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::f...

Why override operator() ?

In the Boost Signals library, they are overloading the () operator. Is this a convention in C++? For callbacks, etc.? I have seen this in code of a co-worker (who happens to be a big Boost fan). Of all the Boost goodness out there, this has only led to confusion for me. Any insight as to the reason for this overload? ...

Retaining functors as variables

I'm working on a resource management class and want to have the user provide a functor to a "ReleaseResource" method as part of the resource manager's constructor. From there when a resource is requested that functor will be provided as the deleter for the shared_ptr that I will be returning so that the appropriate method will be called ...

How to"adapt" functors to use with map/multimap?

I'm having difficulty in using std::for_each and other algorithms with a multimap, and want to know if someone could help me developing a functor that could pass the appropriate parameter to the "generic" functions. My specific problem with map/multimap is that their iterators evaluate to a std::pair instead of the contained value (I me...

I need to create a simple callback in c++? Should I use boost::function?

Suppose I have some code like this: class Visitor { public: Visitor(callBackFunction) {} void visit() { //do something useful invokeCallback(); } } class ClassThatCanBeVisited { Visitor &visitor; public: ClassThatCanBeVisited(Visitor &_visitor) : visitor(_visitor){} void s...

Functor class doing work in constructor

I'm using C++ templates to pass in Strategy functors to change my function's behavior. It works fine. The functor I pass is a stateless class with no storage and it just overloads the () operator in the classic functor way. template <typename Operation> int foo(int a) { int b=Operation()(a); /* use b here, etc */ } I do this often, a...

Functors in Ocaml

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 modu...

Best Java Functor lib: JGA, commons functor, mango, or... ?

I am interested in using functors (function objects) in Java. With quick googling I found these 3 packages: Java Generics Algorithms: http://jga.sourceforge.net/ Commons functor: http://commons.apache.org/sandbox/functor/ Mango: http://www.jezuk.co.uk/cgi-bin/view/mango and of 3, JGA seemed like it might have the best design. But I s...

boost lambda collection size evaluation

I have a function of the form: void DoSomething(const boost::function<bool ()>& condition, other stuff); This function does some work and returns only when the condition is true. The condition has been expressed as a functor argument because I want to supply different conditions at different call sites. Now, this is fairly straightf...

Why this sample works?

> typedef pair<double, double> dd; const double epsilon = 1e-6; struct sort_by_polar_angle { dd center; // Constuctor of any type // Just find and store the center template<typename T> sort_by_polar_angle(T b, T e) { int count = 0; center = dd(0,0); while(b != e) { ...

reusable condition/expression classes

Hi, 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 cla...

zero Functor construction and overhead even with new and delete?

If I have a functor class with no state, but I create it from the heap with new, are typical compilers smart enough to optimize away the creation overhead entirely? This question has come up when making a bunch of stateless functors. If they're allocated on the stack, does their 0 state class body mean that the stack really isn't change...

Call member functions of members of elements of a container with for_each?

Confusing title, hopefully some code will clarify: struct MyNestedType { void func(); }; struct MyType { MyNestedType* nested; } std::vector<MyType> vec; // ... populate vec // I want something approximating this line, but that doesn't use made-up C++! std::for_each(vec.begin(), vec.end(), std::mem_fun_ref(&MyType::nested->f...

Nesting C++ Template Definitions

I'm abusing C++ templates a little and I'm having trouble figuring something out. Let's say I have two types that really should be inherited from a base type, but for speed reasons, I can't afford to have the virtual function overhead (I've benchmarked it, and virtual calls ruin things for me!). First, here are the two classes I have ...

std::sort without functors

I have a question regarding the std::sort algorithm. Here is my test code: struct MyTest { int m_first; int m_second; MyTest(int first = 0, int second = 0) : m_first(first), m_second(second) { } }; int main(int argc,char *argv[]) { std::vector<MyTest> myVec; for(int i = 0; i < 10; ++i) { myVec.pus...

c++ functor and function templates

consider this simple and pointless code. #include <iostream> struct A { template<int N> void test() { std::cout << N << std::endl; } }; int main() { A a; a.test<1>(); } It is a very simple example of a function template. What if however, I wanted to replace A::test with an overloaded operator() to make it a ...

Where can I find, or how can I create an elegant C++ member function template wrapper mechanism without resporting to boost?

I want to be able to templatize a class on a member function without needing to repeat the arguments of the member function -- i e, derive them automatically. I know how to do this if I name the class based on how many arguments the function takes, but I want to derive that as well. Something like this, although this doesn't work (at le...

Template functor error in g++

I've got some code where I would like to build a vector of elements using the mapped values in a map. The code below works fine in Visual Studio (and seems legit as far as I can tell), but g++ disagrees. template<class PAIR> typename PAIR::second_type foo(const PAIR& arg) { return (arg.second); } class A { private: typedef std...

STL string comparison functor

Hi all! I have the following functor: class ComparatorClass { public: bool operator () (SimulatedDiskFile * file_1, SimulatedDiskFile * file_2) { string file_1_name = file_1->getFileName(); string file_2_name = file_2->getFileName(); cout << file_1_name << " and " << file_2_name << ": "; if (file_1_name <...