functor

What is meant by delegates in C++?

What is mean by delegates in c++, does sort function in c/c++ which takes a compare function/functor as last parameter is a form of delegate? ...

In Functional Programming, what is a functor?

I've come across the term 'Functor' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical descriptions (see the Wikipedia article) or incredibly vague descriptions (see the section ...

Can a functor retain values when passed to std::for_each?

According to the first answer to this question, the functor below should be able to retain a value after being passed to foreach ( I couldn't get the struct Accumulator in the example to compile, so built a class). class Accumulator { public: Accumulator(): counter(0){} int counter; void operator()(const Card...

SIMD or not SIMD - cross platform

I need some idea how to write a C++ cross platform implementation of a few parallelizable problems in a way so I can take advantage of SIMD (SSE, SPU, etc) if available. As well as I want to be able at run time to switch between SIMD and not SIMD. How would you suggest me to approach this problem? (Of course I don't want to implement th...

C++ functor to output iterator adapter

Given a functor appropriate for use with std::for_each and friends: template <typename T> struct Foo { void operator()(T const& t) { ... } }; std::for_each(v.begin(), v.end(), Foo<Bar>()); Is there some standard way to convert this into an output iterator appropriate for use with std::copy and friends? (or the opposite adaptatio...

In C++ what does it mean for a compiler to "inline" a function object?

In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them. I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help! ...

C++ templated functor in lambda expression

This first piece has been solved by Eric's comments below but has led onto a secondary issue that I describe after the horizontal rule. Thanks Eric! I'm trying to pass a functor that is a templated class to the create_thread method of boost thread_group class along with two parameters to the functor. However I can't seem to get beyond m...

C++: create betwen-startups relevent functor list

I create something like a list of functors (functions pointers). Then I write them in binary form into file. The problem is, that, functor - is a simple function pointer. (correct me if I'm wrong.) But the address of function is different from one run to another. So, the question - is there any way to create list of functors that will b...

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

Visual Studio 2010 and std::function

I have this code: #include <iostream> #include <functional> struct A { int operator()(int i) const { std::cout << "F: " << i << std::endl; return i + 1; } }; int main() { A a; std::tr1::function<int(int)> f = std::tr1::ref(a); std::cout << f(6) << std::endl; } The aim is to pass the functor object...

Defining < for STL sort algorithm - operator overload, functor or standalone function?

I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class. For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either: a. Define a comparison operator overl...

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). #...

What's the boost way to create a functor that binds out an argument

I have need for a function pointer that takes two arguments and returns a string. I would like to pass an adapter that wraps a function that takes one argument, and returns the string (i.e. discard one of the arguments). I can trivially build my own adapter, that takes the 2 arguments, calls the wrapped function passing just the one ar...

Can I write functors using a private nested struct?

Given this class: class C { private: struct Foo { int key1, key2, value; }; std::vector<Foo> fooList; }; The idea here is that fooList can be indexed by either key1 or key2 of the Foo struct. I'm trying to write functors to pass to std::find_if so I can look up items in fooList by each ...

C++: Template functor cannot deduce reference type

I've got a functor f, which takes a function func and a parameter t of the same type as func. I cannot pass g to f because of compilation error (no matching function for call to f(int&, void (&)(int&)) ). If g would take non-reference parameter g(int s), compilation finishes. Or if I manually specify template parameter f<int&>(i, g), com...

Problems with passing an anonymous temporary function-object to a templatized constructor.

I am trying to attach a function-object to be called on destruction of a templatized class. However, I can not seem to be able to pass the function-object as a temporary. The warning I get is (if the comment the line xi.data = 5;): warning C4930: 'X<T> xi2(writer (__cdecl *)(void))': prototyped function not called (was a variab...

C++: get const or non-const reference type from trait

I am writing a functor F which takes function of type void (*func)(T) and func's argument arg. template<typename T> void F(void (*func)(T), WhatTypeHere? arg) { func(arg); } Then functor F calls func with arg. I would like F not to copy arg, just to pass it as reference. But then I cannot simply write "void F(void (*func)(T), T&)"...

Is there any standard delete functor?

I am looking for a functor that deletes its argument: template<class T> struct delete_functor { void operator()(T* p) { delete p; } }; Is there something like this in std, tr1 or boost? ...

C++ struct sorting error

I am trying to sort a vector of custom struct in C++ struct Book{ public:int H,W,V,i; }; with a simple functor class CompareHeight { public: int operator() (Book lhs,Book rhs) { return lhs.H-rhs.H; } }; when trying : vector<Book> books(X); ..... sort(books.begin(),books.end(), CompareHeight()); it gives me e...

Is it possible to use boost::bind to effectively concatenate functions?

Assume that I have a boost::function of with an arbitrary signature called type CallbackType. Is it possible to use boost::bind to compose a function that takes the same arguments as the CallbackType but calls the two functors in succession? I'm open to any potential solution, but here's a... ...Hypothetical example using some magi...