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
7
votes
0 answers

How does std::function store the context of a lambda?

Given a lambda function with a captured variable, like [&x] (int y) { x += y; } where is the context (here a reference to some variable int x defined anywhere within the lexical context) stored when I pass this lambda to a variable of type…
leemes
  • 44,967
  • 21
  • 135
  • 183
7
votes
1 answer

c++ closure and std::function

I'm trying to figure out what goes on under the hood of std::function when used in combination with closures. I'm not able to wrap my head around it yet, for example: what constructor is being called? Can anybody post a working example of a…
Corno
  • 5,448
  • 4
  • 25
  • 41
7
votes
1 answer

Can std::async call std::function objects?

Is it possible to call function objects created with std::bind using std::async. The following code fails to compile: #include #include #include using namespace std; class Adder { public: int add(int x, int y)…
Huhwha
  • 575
  • 5
  • 15
7
votes
2 answers

Collection of std::functions with different arguments

I am trying to write a simple dispatcher, the user code can attach callbacks to it. Each event has a known signature, and the user code will need to call dispatch with the right number and argument types. This is managed by the variadic arguments.…
Alex Darsonik
  • 597
  • 5
  • 18
7
votes
1 answer

Is there something similar to std::function before C++11?

What construct should be used as surrogate for std::function<> when C++11 is not available ? The alternative should basically allow to access private member functions of one class from another class like in the example below (other features of…
andrsmllr
  • 1,231
  • 18
  • 39
7
votes
2 answers

How to do this with std::bind?

(Note: As should already be clear from the tags, this is strictly C++03. Yes, I know, lambda makes all this pain go away (and brings in new kinds, I bet), but this is an embedded system, with an OS version from the 90s, and I am told I should be…
sbi
  • 219,715
  • 46
  • 258
  • 445
7
votes
1 answer

GCC bug or not : default std::function?

How to specify a default function as a parameter of a class member ? A current example derived from my code is : #include #include template struct C { static T test(std::function f = [](int i){return…
Vincent
  • 57,703
  • 61
  • 205
  • 388
6
votes
3 answers

static member std::function of template class gets empty despite initalization

Consider the following class: template struct Test { Test() { if (!f) { f = []() { std::cout << "it works\n"; }; initialized = true; } } static void check() { if (f) f(); …
miloszmaki
  • 1,635
  • 15
  • 21
6
votes
1 answer

small object optimization useless in using std::function

Many topics told us that use small object like lambda expression could avoid heap allocation when using std::function. But my study shows not that way. This is my experiment code, very simple #include #include using…
wendi
  • 73
  • 5
6
votes
1 answer

std::bind to std::function conversion problem

Trying to get the function object from std::bind() using following code: driver_manager driverManager(); std::function fn = std::bind(&driver_manager::test_callback,…
6
votes
1 answer

Passing function into function with std::function and template argument

I am trying to pass a pointer to the predicate function into the Foo and Bar functions. The Bar function works correctly, but the Foo function raises a compile-time error: error: no matching function for call to Foo(bool (&)(int)) Why does…
6
votes
2 answers

Why does template parameter unpacking sometimes not work for std::function?

I encountered a problem. When I use something like std::function it doesn't work, but std::function does work. This is under Clang 8.0; none of it works under GCC. Here is the example: #include template
Wang
  • 7,250
  • 4
  • 35
  • 66
6
votes
1 answer

ADL with std::function: Can functions taking std::function objects be found via the types in the std::function's argument list?

Consider the following code snippet: #include namespace ns { struct Arg{}; using Func = std::function; Func operator+(Func lhs, Func rhs) { return [lhs, rhs](Arg arg) { return lhs(arg) +…
6
votes
2 answers

Generic std::function to store a generic lambda

When generic lambda is stored as a std::function, we need to provide a concrete type, something like, std::function thus binding to a specific type, The following declaration: std::function throws a compiler error. I…
Rajesh
  • 356
  • 1
  • 5
  • 15
6
votes
1 answer

reinterpret_cast std::function* to and from void*

I'm suffering a segfault in a plugin when I call a std::function in it passed from the main executable, via converting it's address to/from void*. I can reproduce the problem in a few self-contained lines: #include #include…
cmannett85
  • 21,725
  • 8
  • 76
  • 119