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
8
votes
3 answers

Can std::function be serialized?

This is a theoretical question. Say there are some objects which among others contain lists of callback functions subscribed to events of those objects. Now we want to store those objects on disk. Is a std::function serializable?
danijar
  • 32,406
  • 45
  • 166
  • 297
8
votes
1 answer

Store and passing std::function's - by value or by reference?

I'm having trouble knowing when to pass/store std::function objects by value or reference, or if I need to use move semantics somehow. I have a struct that stores two std::function objects: struct Control{ char key; std::function
Eric B
  • 4,367
  • 5
  • 33
  • 43
8
votes
3 answers

Marshal::GetFunctionPointerForDelegate: should I release its result?

I'm converting a managed System.Action to an unmanaged std::function inside a C++/CLI project; should I release the given IntPtr after using the callback, or is it unnecessary? void MyClass::Execute(System::Action^ callback) { IntPtr…
Notoriousxl
  • 1,540
  • 1
  • 16
  • 27
8
votes
1 answer

Algorithm function: Make it a template or take a std::function parameter?

I have a C++ class called Graph, and it has an algorithm method for_each_node(). I can either make it a template, like this: template UnaryFunction Graph::for_each_node (UnaryFunction f) { /* ... */ } or make it use…
8
votes
2 answers

std::function template argument resolution

I am currently working on a library where I am chaining function objects. I am creating a function template that takes a callable object (std::function at the moment) and is parametrized on the output and input type of the function. Here is a…
Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51
7
votes
1 answer

Using std::function for an API (across module boundaries)

Pretty sure I know the answer to this one (thinking no), but could one safely accept/return std::function by value in an API (across module boundaries)? I'm thinking 'no' as I don't think there are any guarantees that one vendor's std::function…
stinky472
  • 6,737
  • 28
  • 27
7
votes
4 answers

How can I prevent implicit conversion among `std::function`s with differenent argument types?

I'm trying to bind some ta-lib functions and then callback. Here is the simplified sample code: #include #include #include struct DataChunk { // ... }; typedef uint64_t idx_t; template
code1704
  • 73
  • 4
7
votes
1 answer

Why `std::function::operator=(F &&)` is required to make a temporary `std::function`?

Apparently std::function::operator=(F &&f) is required to behave exactly as std::function(std::forward(f)).swap(*this);. Unless I'm missing something, this definition causes some superfluous moving: #include #include…
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
7
votes
1 answer

C++11 binding std function to a overloaded static method

this question seems a little bit silly to me but I can't find any similar question so sorry if it's trivial let's say we have a struct: struct C { static void f(int i) { std::cerr << (i + 15) << "\n"; } static void f(std::string s) { std::cerr…
user2717954
  • 1,822
  • 2
  • 17
  • 28
7
votes
2 answers

Possible to copy std::function containing lambda with default parameters?

Is there any way to recover type information from a lambda with default parameters stored in a std::function that does not have those parameters in its type? std::function f1 = [](int i = 0){}; std::function f2 = [](int i =…
7
votes
2 answers

std::function lambda optimization

std::function is known to have performance issues because it may do heap allocations. Admitted, if you are being 100% honest, one heap allocation should hardly be a problem in most cases... but let's just assume doing a heap allocation is…
Damon
  • 67,688
  • 20
  • 135
  • 185
7
votes
3 answers

catch std::function allocations at compile time

I want to only allow use of std::function in my code base if it does not do any allocations. To this end I can write something like the function below and only use it to create my function instances: template< typename…
David Woo
  • 749
  • 4
  • 13
7
votes
2 answers

How do I store a vector of std::bind without a specific case for the template?

After going though a question on std::bind, I was wondering if it was possible to hold a vector of functions created by std::bind so I can avoid using std::function and its heavyweight wrapping. #include #include #include…
CinchBlue
  • 6,046
  • 1
  • 27
  • 58
7
votes
1 answer

Why do I get the error 'vector iterators incompatible'?

I am writing a small UI for my program. I have the method onMouseMotion(), which I can call in one of two ways (see code); if I call it through std::function, then != operator in the for loop stop condition produces the run-time exception vector…
Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
7
votes
3 answers

std::function and shared_ptr

I have been using Loki's Functor for a while and I recently asked a question about it (still unanswered...) I have been told to use std::function, but I prefer Loki's implementation of Functor since it also work with all sorts of pointers as…
Athanase
  • 933
  • 9
  • 25