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
12
votes
2 answers

Call to lambda is ambiguous despite explicitly stating the return type

An overloaded function should take both functors in, given the type of the lambda is decidable ( castable to an std::function (please correct me if I'm wrong ). The question is: Why is there a compile error below, despite the lambda type being…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46
12
votes
4 answers

What is the most efficient way to pass a non generic function?

I am learning functional programming in C++. My intention is to pass a non generic function as argument. I know about the template method, however I would like to restrict the function signature as part of the API design. I worked out 4 different…
Afoxinabox
  • 319
  • 2
  • 9
12
votes
1 answer

Does std::function allow an implicit cast from reference to copy in its return type?

In the code snipped below, the compiler silently casts the return-by-copy function pointer into a return-by-const-reference std::function. When the std::function instance is called, a reference to the copy is returned and the application crashes…
Jacek Sieka
  • 603
  • 6
  • 13
12
votes
2 answers

How do I `std::bind` a non-static class member to a Win32 callback function `WNDPROC`?

I'm trying to bind a non-static class member to a standard WNDPROC function. I know I can simply do this by making the class member static. But, as a C++11 STL learner, I'm very interested in doing it by using the tools under the
hkBattousai
  • 10,583
  • 18
  • 76
  • 124
12
votes
2 answers

Is there a use case for std::function that is not covered by function pointers, or is it just syntactic sugar?

The notation for std::function is quite nice when compared to function pointers. However, other than that, I can't find a use case where it couldn't be replaced by pointers. So is it just syntactic sugar for function pointers?
static_rtti
  • 53,760
  • 47
  • 136
  • 192
11
votes
1 answer

std::function const correctness

Suppose I have a callable type like so: struct mutable_callable { int my_mutable = 0; int operator()() { // Not const return my_mutable++; } }; Note that mutable_callable has a non-const operator() that modifies a member…
DarthRubik
  • 3,927
  • 1
  • 18
  • 54
11
votes
1 answer

Why can std::function be constructed with a lambda with a different return type?

The following compiles fine: #include int main() { std::function f = []() -> int {return 1;}; const int& r = f(); // r is a dangling reference return 0; } How come it's possible to set an std::function with a…
Danra
  • 9,546
  • 5
  • 59
  • 117
11
votes
2 answers

Convert lambda with capture clause stored in std::function to raw function pointer

Since my last recent question was unfortunately worded and resulted in a solution to another problem then mine, here I will try to formulate my actual problem in a clear way. Before we start, as a sidenote, I am integrating the Javascript Engine V8…
danijar
  • 32,406
  • 45
  • 166
  • 297
11
votes
2 answers

Clang++ generates executable that leaks memory, about std::function and lambda

Clang++ 3.2 on ArchLinux (i686) is used to build the following C++11 codes #include #include typedef std::function Action; typedef std::function Generator; Action act(Generator const& gen) { return…
neuront
  • 9,312
  • 5
  • 42
  • 71
11
votes
4 answers

how to avoid static member function when using gsl with c++

I would like to use GSL within a c++ class without declaring member functions as static. The reason for this is because I don't know them too well and I'm not sure about thread safety. From what I read, std::function might be a solution but I'm not…
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
11
votes
2 answers

std::function copying parameters?

My code: #include #include using namespace std; struct A { A() = default; A(const A&) { cout << "copied A" << endl; } }; void foo(A a) {} int main(int argc, const char * argv[]) { std::function f =…
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
10
votes
2 answers

Lambda to function using generalized capture impossible?

A lambda can be easily converted to std::function though this seems to be impossible when the lambda uses generalized capture with a unique_ptr. Likely an underlying std::move is missing. Is there a workaround for this or is this a known…
10
votes
4 answers

std::bind to a std::variant containing multiple std::function types

I'm playing around with callback functions and wish to register multiple functions via std::bind that differ in signatures (altough they all return void). Assigning the result of the std::bind to the std::variant yields in a "conversion to…
Tmplt
  • 127
  • 1
  • 7
10
votes
3 answers

std::function const correctness not followed

I was surprised to find this code compiles: #include struct Callable { void operator() () { count++; } void operator() () const = delete; int count = 0; }; int main() { const Callable counter; // counter();…
user10579912
  • 101
  • 1
  • 3
10
votes
2 answers

Function template argument deduction (class vs funtion template)

Could you help me understand why the argument deduction works for the class template and does not work for the function template? If I understand correctly, the class template defines a function, so when I call it is possible for the compiler to…