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.

705 questions
169
votes
7 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,057
  • 4
  • 22
  • 29
151
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
  • 4,824
  • 5
  • 27
  • 43
107
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
  • 3,834
  • 4
  • 22
  • 34
77
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…
73
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
  • 22,097
  • 9
  • 57
  • 125
65
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,038
  • 8
  • 18
65
votes
2 answers

Usage and Syntax of std::function

It is necessary to 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
  • 765
  • 1
  • 7
  • 13
45
votes
7 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
  • 1,723
  • 1
  • 19
  • 40
40
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
  • 493
  • 5
  • 5
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
  • 45,296
  • 16
  • 80
  • 161
34
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,280
  • 3
  • 15
  • 30
34
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
  • 50,257
  • 51
  • 171
  • 339
33
votes
3 answers

How are C++11 lambdas represented and passed?

Passing a lambda is really easy in c++11: func( []( int arg ) { // code } ) ; But I'm wondering, what is the cost of passing a lambda to a function like this? What if func passes the lambda to other functions? void func( function< void (int arg)…
bobobobo
  • 57,855
  • 58
  • 238
  • 337
28
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,431
  • 5
  • 26
  • 31
27
votes
3 answers

What is the return type of a lambda expression if an item of a vector is returned?

Consider the following snippet: #include #include #include int main() { std::vectorv = {0,1,2,3,4,5,6}; std::function f = [&v](int i) { return v[i];}; std::function
Jing Li
  • 599
  • 4
  • 14
1
2 3
46 47