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

Template type deduction with std::function

I have discovered the following behaviour with std::function and type deduction, which was unexpected for me: #include template void stdfunc_test(std::function func) {}; int test_func(int arg) { return arg +…
redspah
  • 257
  • 3
  • 9
10
votes
3 answers

Using std::function and std::bind to store callback and handle object deletion.

I want to implement a manager that stores callbacks to member functions of polymorphic classes using C++11. The issue is that I am not sure how to handle the case where the object that the member belongs to gets deleted or should be deleted and I…
CJCombrink
  • 3,738
  • 1
  • 22
  • 38
10
votes
1 answer

How to use SWIG to wrap std::function objects?

I have seen quite a few similar questions, but have not found a solution to my particular problem. I am attempting to SWIGify some C++11 code that uses std::function, so I can use it in my Java application. I have encountered shared pointers like…
Denial
  • 363
  • 1
  • 5
  • 12
10
votes
3 answers

Create a "do-nothing" `std::function` with any signature?

I would like to create a simple no-op std::function object with an arbitrary signature. To that end, I've created two functions: template std::function GetFuncNoOp() { // The…
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
10
votes
2 answers

What is the difference between std::function and std::mem_fn

I am having trouble figuring out the difference between the two function wrappers std::function and std::mem_fn. From the description, it seems to me that std::function does everything std::mem_fn does and more. In which instance would one use…
Vicks
  • 111
  • 1
  • 6
10
votes
4 answers

How to implement strategy pattern in C++ with std::function

I'm reasing about the best way to implement the strategy pattern in C++. Up to now, I've always used the standard way, where the context has a pointer to the base strategy class as follows: class AbstractStrategy{ public: virtual void exec()…
gcswoosh
  • 1,279
  • 1
  • 15
  • 31
10
votes
1 answer

assigning lambda to std::function

Why is the 2nd assignment allowed, when the inferred return type is std::nullptr_t? With function pointers this is forbidden. And why doesn't the 2nd lambda run? #include #include int main() { std::function f; …
Valentin Milea
  • 3,186
  • 3
  • 28
  • 29
10
votes
3 answers

g++: std::function initialized with closure type always uses heap allocation?

Some sources on the Internets (specifically this one) says that std::function use small-closure optimizations, e.g. it do not allocate heap if closure size is lower than some amount of data (link above indicates 16 bytes for gcc) So I went digging…
Alex I.
  • 191
  • 1
  • 6
9
votes
2 answers

Question about custom conversion of "lambda []void ()->void"

TestCase2 and TestCase3 can compile normally. However, in TestCase1 I get the following error: E0312, Custom conversion from "lambda []void ()->void" to "EventHandler" is not appropriate. Why am I getting this error? I want to know how to…
Sitanimyu
  • 93
  • 3
9
votes
4 answers

Why can't I use std::function as a std::set or std::unordered_set value type?

Why can't I have a std::set or std::unordered_set of std::functions? Is there any way to get it to work anyway?
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
9
votes
2 answers

Using std::function with templates

So in it's most distilled form I have something like this going on, template bool f(const T &a, const T &b, std::function func) { return func(a,b); } template bool g(const T &a, const T &b) { …
Arii
  • 349
  • 2
  • 10
9
votes
3 answers

Why is compiling a code affecting lambda to std::function so slow, in particular with Clang?

I discovered that compile time of a relatively small amount of code, converting lambda functions to std::function<> values, can be very high, in particular with Clang compiler. Consider the following dummy code that creates 100 lambda functions: #if…
prapin
  • 6,395
  • 5
  • 26
  • 44
9
votes
1 answer

compiler cannot deduce overload of std::max

With my compiler typedef const double&(*fT)(const double&, const double&); typedef std::function std_func; fT f1 = std::max; //(1) std_func f2 =…
jimifiki
  • 5,377
  • 2
  • 34
  • 60
9
votes
1 answer

Why is the copy ctor invoked twice when using std::bind?

I am playing around with std::function and std::bind to understand how arguments are copied around and if I can save some of the copy operations. I understand that when using std::bind, the arguments are passed by value and not reference (unless…
urnonav
  • 191
  • 7
9
votes
3 answers

C++14: Generic lambda with generic std::function as class member

Consider this pseudo-snippet: class SomeClass { public: SomeClass() { if(true) { fooCall = [](auto a){ cout << a.sayHello(); }; } else { fooCall = [](auto b){ cout <<…
Juergen
  • 3,489
  • 6
  • 35
  • 59