functors

C++ Functors - and their uses.

I keep hearing a lot about functors in C++, can someone give me an overview as to what they are and in what cases they would be useful? ...

g++ rejects my simple functor with "expected a type, got 'xyz'"

I've been playing about with functors in C++. In particular, I've got a vector of pairs I'd like to sort by the first element of the pair. I started off writing a completely specialised functor (i.e. something like "bool MyLessThan(MyPair &lhs, MyPair &rhs)"). Then, just because this sort of stuff is interesting, I wanted to try writing ...

Functors when should I use them whats their intended use.

I Just can't seem to wrap my head around them. As I understand it's dynamicly adding logic to a class. Are classes within the framework prepaired for this? Why should I just extend the class and add the funtionality to it in the extention. I would be globally accessable and afaik much easier to maintain. I've Read there are 4 functor ...

C++ Functors and Zero

Hello all: First a disclaimer, I am replacing a bunch of code which uses boost::function and boost::bind. However, I am moving to a codebase which does not allow rtti. I would like to keep using boost but don't know if there is a way around this restriction. So..., I am trying to mimic some of its functionality, but much more simplif...

Can ML functors be fully encoded in .NET (C#/F#)?

Can ML functors be practically expressed with .NET interfaces and generics? Is there an advanced ML functor use example that defies such encodings? Answers summary: In the general case, the answer is NO. ML modules provide features (such as specification sharing via signatures [1]) that do not directly map to .NET concepts. However, ...

how to use binder and bind2nd functors?

How to use binder2nd, bind2nd, and bind1st? More specifically when to use them and are they necessary? Also, I'm looking for some examples. ...

C++ functor - unexpected behaviour?

I have written this program, which sorts some ints using a functor: #include<iostream> #include<list> #include<set> using namespace std; struct IntSorter { unsigned int comparisons; IntSorter() { std::cout << "new intsorter" << std::endl; comparisons = 0; } bool operator() (const int &a, const int ...

Function pointers to member functions in C++

I need to call a method that expects a function pointer, but what I really want to pass to it is a functor. Here's an example of what I'm trying to do: #include <iostream> #include "boost/function.hpp" typedef int (*myAdder)(int); int adderFunction(int y) { return(2 + y); } class adderClass { public: adderClass(int x) : _x(x) ...

Would you please explain OCaml functors to me?

Hi! I don't know much about OCaml, I've studied F# for some time and quite understand it. They say that F# misses functor model, which is present in OCaml. I've tried to figure out what exactly functor is, but wikipedia and tutorials didn't help me much. Could you please illuminate that mystery for me? Thanks in advance :) EDIT: I...

Different behavior of functors (copies, assignments) in VS2010 (compared with VS2005)

When moving from VS2005 to VS2010 we noticed a performance decrease, which seemed to be caused by additional copies of a functor. The following code illustrates the problem. It is essential to have a map where the value itself is a set. On both the map and the set we defined a comparison functor (which is templated in the example). #...

How to use objects as modules/functors in Scala?

Hi. I want to use object instances as modules/functors, more or less as shown below: abstract class Lattice[E] extends Set[E] { val minimum: E val maximum: E def meet(x: E, y: E): E def join(x: E, y: E): E def neg(x: E): E } class Calculus[E](val lat: Lattice[E]) { abstract class Expr case class Var(name: String) extends...

Practise Questions for Templates,Functors,CallBack functions in c++?

Hi, I have been reading templates,functors,callback function for the past week and have referred some good books and articles. I however feel that, unless I can get good practice - programming in templates and use functors-callbacks there is no way I can really understand all the concepts or fluently use them while coding. Could any...

doubt in - Function Objects - c++

I have a class class fobj{ public: fobj(int i):id(i) {} void operator()() { std::cout<<"Prints"<<std::endl; } private: int id; }; template<typename T> void func(T type) { type(); } My Doubt is if I invoke func like Method 1: func(fobj(1)); the message I wanted to print is printed. I was alwa...

[Scala] "can't existentially abstract over parameterized type..."

I was messing around with Scala 2.8 for fun and trying to define a pimp which adds an "as" method to type constructors, allowing to convert from one functor to another (please overlook the fact that I'm not necessarily dealing with functors here). So for instance, you could use it like this: val array:Array[T] val list:List[T] = array.a...

Understanding functors in OCaml

Hello, I'm quite stuck with the following functor problem in OCaml. I paste some of the code just to let you understand. Basically I defined these two modules in pctl.ml: module type ProbPA = sig include Hashtbl.HashedType val next: t -> (t * float) list val print: t -> float -> unit end module type M = sig type s val s...

C++ Templated Functor (based on Modern C++ Design) compile error

Based on chapter 5 (Generalized Functors) from the book "Modern C++ Design," I'm trying to write a Functor template. Before asking me "why don't I just use Boost's bind or Loki straight up?" the simple answer is "because I want to learn." That being said, I have followed the book, and also used the example code as a reference, but I ke...