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
20
votes
3 answers

Using `std::function` to call non-void function

A while ago I used std::function pretty much like this: std::function func = [](int i) -> int { return i; }; Basically, I did this because I wanted to store different function objects in a std::function, but I didn't want to restrict the…
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
20
votes
2 answers

C++ std::function-like template syntax

In C++11 you can instantiate std::function like this: std::function f1; std::function f2; //and so on But while there is plenty of info on variadic templates on the web, I fail to find any articles on how…
Smiles
  • 1,733
  • 2
  • 11
  • 13
20
votes
7 answers

Binding to a weak_ptr

Is there a way to std::bind to a std::weak_ptr? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed. I know how to create a std::function using a shared_ptr: std::function
Scotty
  • 2,480
  • 2
  • 16
  • 20
19
votes
4 answers

Syntax of C++ templates with function type parameters

I'm used to seeing syntax like this for function pointers int (*pointer_name) (float, char *); void call_function (void (*)(int), int); In some C++03 functional libraries I see types used this way: abc::function f; In C++11's…
spraff
  • 32,570
  • 22
  • 121
  • 229
19
votes
2 answers

Move constructor called twice when move-constructing a std::function from a lambda that has by-value captures

When move-constructing a std::function object from a lambda, where that lambda has by-value captures, it appears that the move-constructor of the object that is value-captured is called twice. Consider #include #include…
aafulei
  • 2,085
  • 12
  • 27
19
votes
4 answers

how to declare properly the template taking function type as a parameter (like a std::function)

I found out that its not trivial to have a neat syntax like in: std::function If I declare the function as: template class function {}; It would be an ordinary syntax to define function's templated…
barney
  • 2,172
  • 1
  • 16
  • 25
19
votes
1 answer

Converting boost::function to std::function

I'm updating some of our old code to use C++11 features in place of boost equivalents. However not everything is a simple namespace replacement like unordered containers and smart pointers. For example boost::function has methods empty() and clear()…
AJG85
  • 15,849
  • 13
  • 42
  • 50
18
votes
4 answers

Why doesn't std::function participate in overload resolution?

I know that the following code won't compile. void baz(int i) { } void baz() { } class Bar { std::function bazFn; public: Bar(std::function fun = baz) : bazFn(fun){} }; int main(int argc, char **argv) { Bar b; …
mdx
  • 534
  • 3
  • 16
18
votes
1 answer

Call operator with auto return type being chosen instead of constructor when using std::function

The following snippet: #include struct X { X(std::function fn); // (1) X(double, double); // (2) template auto operator()(T const& t) const { // (3) return t.foo(); …
Holt
  • 36,600
  • 7
  • 92
  • 139
18
votes
2 answers

std::bind makes no sense to me whatsoever

The following: #include struct Foo { void bar1() {} void bar2(int) {} void bar3(int, int) {} void bar4(int, int, int) {} }; int main() { Foo foo; auto f1 = std::bind(&Foo::bar1, &foo); auto f2 =…
Robinson
  • 9,666
  • 16
  • 71
  • 115
18
votes
4 answers

C++ How to Reference Templated Functions using std::bind / std::function

If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)? I was given some help about the basic syntax in a post below, to bind to functions with…
e.s. kohen
  • 213
  • 1
  • 4
  • 21
16
votes
2 answers

Why assignment to std::function doesn't compile when it is a member of class X?

The following code doesn't compile: #include struct X { std::function _gen; }; int main() { X x; x._gen = [] { return X(); }; //this line is causing problem! } I don't understand why assignment to x._gen is causing…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
15
votes
3 answers

Why doesn't GCC's std::function use rvalue references to arguments passed by value to pass them between its internal delegates?

First, consider the following code: #include #include struct Noisy { Noisy() { std::cout << "Noisy()" << std::endl; } Noisy(const Noisy&) { std::cout << "Noisy(const Noisy&)" << std::endl; } Noisy(Noisy&&) { std::cout…
Marc Andreson
  • 3,405
  • 5
  • 35
  • 51
15
votes
1 answer

C++11 std::function and perfect forwarding

Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not R operator()(ArgTypes&&...) const; ? One would think that to correctly forward parameters, we need the && and then use…
airman
  • 564
  • 1
  • 4
  • 16
14
votes
7 answers

Why do std::function instances have a default constructor?

This is probably a philosophical question, but I ran into the following problem: If you define an std::function, and you don't initialize it correctly, your application will crash, like this: typedef std::function
Patrick
  • 23,217
  • 12
  • 67
  • 130
1 2
3
56 57