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

Is it safe to change a function pointer (std::function) inside a called function?

I have a std::function pointing to a function. Inside this function I change the pointer to another function. std::function fun; void foo() { std::cout << "foo\n"; } void bar() { std::cout << "bar\n"; fun = foo; } int main()…
user1810087
  • 5,146
  • 1
  • 41
  • 76
14
votes
4 answers

Why can't we trivially copy std::function

The reason for me to ask this is I need to store std::function in a vector, and the in-house vector we have in company basically is doing realloc if it needs more memory. (Basically just memcpy, no copy/move operator involves) This means all the…
Xu Pan
  • 310
  • 3
  • 12
14
votes
3 answers

Odd return behavior with std::function created from lambda (C++)

I'm having trouble with std::functions created from lambdas if the function returns a reference but the return type isn't explicitly called out as a reference. It seems that the std::function is created fine with no warnings, but upon calling it, a…
Kevin
  • 510
  • 4
  • 16
14
votes
3 answers

Converting std::function to std::function

First, I define two classes, which inherits from one another. class A { }; class B : public A { }; Then, I declare a function that uses an std::function : void useCallback(std::function myCallback); Finally, I receive a…
Ecco
  • 1,323
  • 1
  • 11
  • 15
14
votes
2 answers

Is it possible to allow one std::function type accept lambdas with different signatures

I have a higher order function map which is similar to STL for_each, and maps a std::function object over a vector of things. template vector map(function f, vector xs) { vector ret; for (auto &x: xs) …
thor
  • 21,418
  • 31
  • 87
  • 173
14
votes
2 answers

std::function as template parameter

I currently have a map, but for flexibility, I want to be able to assign a lambda expression, returning std::wstring as value in the map. So I created this template class: template class ValueOrFunction { private: …
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
14
votes
6 answers

Default function that just returns the passed value?

As a lazy developer, I like to use this trick to specify a default function: template > void arrange(std::array &x, Function&& f = Function()) { …
Vincent
  • 57,703
  • 61
  • 205
  • 388
14
votes
3 answers

Performance of std::function compared to raw function pointer and void* this?

Library code: class Resource { public: typedef void (*func_sig)(int, char, double, void*); //Registration registerCallback(void* app_obj, func_sig func) { _app_obj = app_obj; _func = func; } //Calling when the time…
balki
  • 26,394
  • 30
  • 105
  • 151
13
votes
1 answer

Why does C++23 std::move_only_function not have deduction guides?

C++23 introduced std::function's cousin std::move_only_function, just like its name, it is a move-only wrapper for move-only callable objects (demo): #include #include int main() { auto l = [p = std::make_unique(0)] {…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
13
votes
2 answers

How to pass std::function with different parameters to same function

I have three functions I'm looking to merge together. Each one takes an std::function as the first parameter, then executes it within a try/catch block. The issue is, there are three different types of functions. Functions with no parameters, those…
Griffort
  • 1,174
  • 1
  • 10
  • 26
13
votes
6 answers

Deleting a std::function object within itself

Is this well defined behavior? #include void foo() { auto f = new std::function; *f = [f]() { delete f; }; (*f)(); f = nullptr; } int main() { foo(); } Using the most recent g++, if I do this within a…
user2005303
13
votes
2 answers

How to create a variadic template function with `std::function` as a function parameter?

How can I create a variadic template function with std::function as a function parameter that accepts a variadic number of arguments? I tried to reduce the problem to a MWE: #include template void run(std::function
13
votes
3 answers

std::function instead of templates for predicates

Many standard library algorithms take predicate functions. However, the type of these predicates is an arbitrary, user-provided template parameter. Why doesn't C++11 specify that these take a specific type, like std::function instead? For…
paul23
  • 8,799
  • 12
  • 66
  • 149
13
votes
1 answer

binding member functions in a variadic fashion

I have a member function with a variable number of parameters, stored in a std::function, and I want to bind the instance and get an independent function object. template void connect(const T& t,…
lucas clemente
  • 6,255
  • 8
  • 41
  • 61
13
votes
3 answers

Deduce template argument from std::function call signature

Consider this template function: template ReturnT foo(const std::function& fun) { return fun(); } Why isn't it possible for the compiler to deduce ReturnT from the passed call signature? bool bar() { /* ... */…
Karl von Moor
  • 8,484
  • 4
  • 40
  • 52