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
9
votes
1 answer

A const std::function wraps a non-const operator() / mutable lambda

Consider the following example: #include #include struct A { int i; void operator()() { std::cout << ++i; } }; void test(std::function const& fun) { fun(); } int main() { const…
rustyx
  • 80,671
  • 25
  • 200
  • 267
9
votes
2 answers

Pass Member Function as Parameter to other Member Function (C++ 11 )

Let's say that I have a class with three member functions, as follows: #include #include class ClassName { public: double add(double a, double b); double intermediate(double a, double b, std::function
JohnTravolski
  • 131
  • 1
  • 1
  • 6
9
votes
3 answers

Expand a type N times in template parameter

I have the following problem: template< std::size_t N > class A { std::function< std::size_t( /*std::size_t,....,std::size_t <- N-times*/) > foo; }; As you can see above, I try to declare an std::function<...> foo as a member of a class A. Here,…
abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30
9
votes
1 answer

Will C++17 template arguments with auto feature allow constrained std::function objects?

With the upcoming C++17 feature of non-type template arguments with auto, will it be possible to implement std::function in such a way as to be able to put, for example, the following functions: bool f(int n, double d) {} bool g(bool…
Davit Tevanian
  • 861
  • 8
  • 16
9
votes
1 answer

What's the point of std::function constructor with custom allocator but no other args?

I'm playing around with std::function and custom allocators but its not behaving as I expected when I don't provide the function with an initial functor. When I provide a custom allocator to the constructor but no initial functor, the allocator is…
David Woo
  • 749
  • 4
  • 13
9
votes
2 answers

Can std::function be used to store a function with variadic arguments

I have a structure that I pass around my application which contains a bunch of callback functions: typedef struct { std::function f1; std::function f2; std::function f3; // ... and so…
Eos Pengwern
  • 1,467
  • 3
  • 20
  • 37
9
votes
3 answers

c++11: How to write a wrapper function to make `std::function` objects

I am trying to write a wrapper make_function, which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair, for a function pointer foo, auto f0 = make_function(foo); creates a std::function…
thor
  • 21,418
  • 31
  • 87
  • 173
9
votes
2 answers

Understanding std::function and std::bind

I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better. For example: void fun() { } void hun(std::string) { } int main() { function g = &fun; //This…
Alex
  • 600
  • 1
  • 5
  • 16
9
votes
1 answer

C++ Dynamically load arbitrary function from DLL into std::function

How can I load an arbitrary dynamic-link library (dll) function into a std::function object using a single function? For example I would like to compile two functions into a dll: // test.dll int plusFive(int value) { return value + 5; } void…
Felix Glas
  • 15,065
  • 7
  • 53
  • 82
8
votes
2 answers

type deduction for std::function argument types with auto adds const

I have a struct with a method called call which has a const overload. The one and only argument is a std::function which either takes a int reference or a const int reference, depending on the overload. The genericCall method does exactly the same…
elfenpiff
  • 105
  • 4
8
votes
1 answer

Capture arguments are copied from rvalue lambda?

Are captured arguments copied during the conversion of lambdas to std::function? I need to convert a lambda that captures a non-copyable type to std::function. So I passed a lambda to std::function as an rvalue, but an error occurred. // Foo is…
user12604069
8
votes
2 answers

How expensive is to copy an std::function?

While std::function is movable, in some cases it's not possible or not convenient. Is there a significant penalty for copying it? Does it possibly depend on the size of the captured variables (if it was created using a lambda expression)? Is it…
Petr
  • 62,528
  • 13
  • 153
  • 317
8
votes
2 answers

Can be std::function inlined or should I use different approach?

I'm working on a complex framework which uses std::function<> as argument of many functions. By profiling i found one of the performance problem the following. Can somebody explain me why the Loop3a is so slow? I expected that the inlining will be…
Radek
  • 518
  • 5
  • 12
8
votes
1 answer

Why is there a difference between std::function constructor or assignment?

The std::function type erasure constructor is defined as: template< class F > function( F f ); The assignment operator is defined as: template< class F > function& operator=( F&& f ); (source cppreference) Why does the constructor gets f by value…
RTempete
  • 245
  • 1
  • 4
8
votes
1 answer

Why std::bind cannot resolve function overloads with multiple arguments?

Consider following example: #include #include using namespace std; void f(int x,int y) {} // void f(int x) {} // if this line is uncommented, code does not compile. int main() { auto functor =…
Ilya Kobelevskiy
  • 5,245
  • 4
  • 24
  • 41