Questions tagged [std-function]

A C++11 class template that is callable like a function, and wraps another callable type and forwards calls to it.

In the words of the C++11 standard std::function is a polymorphic wrapper class that encapsulates arbitrary callable objects. It is polymorphic because an instance of the type std::function<R (A1, A2)> could wrap an object of many different callable types, including:

  • a function pointer of type R (*)(A1, A2)
  • a function pointer of type R (*)(const A1&, const A2&)
  • a function object (a.k.a functor) with a member function such as R C::operator()(A1, A2)
  • a pointer to member function of type R (A1::*)(A2)
  • a lambda [](A1 a1, A2 a2) -> R {...}

std::function is often used to implement generic callbacks or to support passing arbitrary callable types to a function that cannot be written as function template e.g. because it must be virtual.

Use this tag for questions about std::function and std::tr1::function.

844 questions
186
votes
3 answers

Should I pass an std::function by const-reference?

Let's say I have a function which takes an std::function: void callFunction(std::function x) { x(); } Should I pass x by const-reference instead?: void callFunction(const std::function& x) { x(); } Does the answer to this…
Sven Adbring
  • 1,961
  • 2
  • 12
  • 3
181
votes
8 answers

std::function vs template

Thanks to C++11 we received the std::function family of functor wrappers. Unfortunately, I keep hearing only bad things about these new additions. The most popular is that they are horribly slow. I tested it and they truly suck in comparison with…
Red XIII
  • 5,771
  • 4
  • 28
  • 29
178
votes
4 answers

std::function and std::bind: what are they, and when should they be used?

I know what functors are and when to use them with std algorithms, but I haven't understood what Stroustrup says about them in the C++11 FAQ. Can anyone explain what std::bind and std::function are, when they should be used, and give some examples…
Mr.Anubis
  • 5,132
  • 6
  • 29
  • 44
132
votes
3 answers

How to properly check if std::function is empty in C++11?

I was wondering how to properly check if an std::function is empty. Consider this example: class Test { std::function eventFunc; void registerEvent(std::function e) { eventFunc = e; } void…
NightElfik
  • 4,328
  • 5
  • 27
  • 34
127
votes
3 answers

What is the purpose of std::function and how to use it?

It is necessary for me to use std::function but I don't know what the following syntax means. std::function f_name = []() { FNAME(); }; What is the goal of using std::function? Is it to make a pointer to a function?
user2982229
  • 1,395
  • 2
  • 11
  • 13
82
votes
6 answers

C++11 std::set lambda comparison function

I want to create a std::set with a custom comparison function. I could define it as a class with operator(), but I wanted to enjoy the ability to define a lambda where it is used, so I decided to define the lambda function in the initialization list…
77
votes
5 answers

Should I copy an std::function or can I always take a reference to it?

In my C++ application (using Visual Studio 2010), I need to store an std::function, like this: class MyClass { public: typedef std::function MyFunction; MyClass (Myfunction &myFunction); private: MyFunction…
Patrick
  • 23,217
  • 12
  • 67
  • 130
75
votes
6 answers

C++11 lambda as data member?

Can lambdas be defined as data members? For example, would it be possible to rewrite the code sample below using a lambda instead of a function object? struct Foo { std::function bar; }; The reason I wonder is because the following…
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
66
votes
9 answers

Functional programming in C++. Implementing f(a)(b)(c)

I have been getting into the basics of functional programming with C++. I am trying to make a function f(a)(b)(c) that will return a + b + c. I successfully implemented the function f(a)(b) which returns a + b. Here is the code for…
Gigaxel
  • 1,058
  • 1
  • 9
  • 20
58
votes
6 answers

Comparing std::functions for equality?

How can I compare two C++11 std::functions with operator==, and return true if both of said functions refer to the same function pointer?
JesseTG
  • 2,025
  • 1
  • 24
  • 48
43
votes
4 answers

Isn't the template argument (the signature) of std::function part of its type?

Given the following code, what is the reason behind the ambiguity? Can I circumvent it or will I have to keep the (annoying) explicit casts? #include using namespace std; int a(const function& f) { return f(); } int…
GhostlyGhost
  • 523
  • 5
  • 5
43
votes
5 answers

std::function fails to distinguish overloaded functions

I am trying to understand why std::function is not able to distinguish between overloaded functions. #include void add(int,int){} class A {}; void add (A, A){} int main(){ std::function func = add; } In the…
MS Srikkanth
  • 3,829
  • 3
  • 19
  • 33
43
votes
2 answers

Lambda of a lambda : the function is not captured

The following program do not compile : #include #include #include #include #include #include void asort(std::vector& v, std::function f) { …
Vincent
  • 57,703
  • 61
  • 205
  • 388
41
votes
4 answers

C++ lambda capture this vs capture by reference

If I need to generate a lambda that calls a member function, should I capture by reference or capture 'this'? My understanding is that '&' captures only the variables used, but 'this' captures all member variable. So better to use '&'? class MyClass…
vikky.rk
  • 3,989
  • 5
  • 29
  • 32
37
votes
1 answer

Can a std::function store pointers to data members?

From cppreference, I found that: Class template std::function is a general-purpose polymorphic function wrapper. Instances of std::function can store, copy, and invoke any Callable target -- functions, lambda expressions, bind expressions, or other…
skypjack
  • 49,335
  • 19
  • 95
  • 187
1
2 3
56 57