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
6
votes
1 answer

remove an item from a vector of std::function

I'm attempting to write a an observer pattern in c++ so I have a map that contains eventname -> vector of callback functions The callback functions are stored in a vector as std::function so the map looks…
stack user
  • 835
  • 1
  • 9
  • 28
6
votes
2 answers

Prevent std::function in gcc from allocating memory or increase threshhold

Is there any way to prevent std::function in gcc from dynamically allocating memory for larger function objects? I would have expected the following code to work without dynamic allocation: #include #include // replace…
Blackclaws
  • 436
  • 5
  • 20
6
votes
3 answers

Return initializer list instead of vector in std::function

Edit: It is not duplicated of the linked question (which is mine also). Here all the return types are std::vector. I do not want to return an initializer-list. I want to fill the returned std::vector by initializer-list directly Let us take…
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
6
votes
4 answers

Create a std::function type with limited arguments

Given the type of a callable function C, I want to get at compile time a std::function; the type of which: has the same return type of function C the argument types are the first N argument types of function C This means that, for a given type…
skypjack
  • 49,335
  • 19
  • 95
  • 187
6
votes
3 answers

std::bind and function templates

I am currently trying to use std::bind to create a std::function from the function template template void printRange(Iterator first, Iterator last) { std::copy(first, last, std::ostream_iterator
Marcel
  • 616
  • 1
  • 5
  • 15
6
votes
1 answer

Why should bind() be deprecated?

Reading the proposal for C++17 about removing some deprecated, old and unused parts of the standard (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4190.htm) i find section D.9 a bit strange: D.9 "Binders" [depr.lib.binders] This defines…
Martin G
  • 17,357
  • 9
  • 82
  • 98
6
votes
2 answers

Using std::function in std::map

I want use a std::function in std::map, follow the code: #include #include class MyClass { ... std::function Callback(); std::map my_map; ... } std::map receive a Key and a T, but a…
user3854612
  • 127
  • 1
  • 1
  • 7
6
votes
1 answer

Proxying a std::function to a C function that wants an array of arguments

I'm dealing with a C system that offers a hook of this form: int (*EXTENSIONFUNCTION)(NATIVEVALUE args[]); It's possible to register an EXTENSIONFUNCTION and the number of arguments it takes. My idea was that I'd make a class Extension to wrap up…
6
votes
2 answers

Should std::function assignment ignore return type?

Is the code below valid C++ according to the C++11 or C++14 standard? #include int ReturnInt() { return 5; } int main( int argc, char **argv ) { std::function< void () > BoundType = ReturnInt; return 0; } The code compiles fine…
fun4jimmy
  • 672
  • 4
  • 16
6
votes
1 answer

Function Composition Operator

As a small exercise in functional programming in C++, I want to overload the operator* to allow me to compose 2 functions. What stumps me is that if I define my operator like this: std::function operator*(std::function
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
6
votes
1 answer

Define std::hash

I need to create a templated class that can hold pointers to elements of type T and then performs functions on them. The functions will come from different places, so I need a container to store them, so I can call them later. I decided to use an…
Andrew Larsson
  • 860
  • 1
  • 18
  • 23
6
votes
2 answers

Initialize class containing a std::function with a lambda

I created a template class containing a std::function as a member the following way: template class Foo { private: std::function _func; public: Foo(const std::function& func): …
Morwenn
  • 21,684
  • 12
  • 93
  • 152
6
votes
1 answer

Checking if an std::function is assigned to a nullptr

I am wondering if there is any way to check if a function pointer you assigned into an std::function was a nullptr. I was expecting the !-operator to do it, but it only seems to work when the function has been assigned something of type…
Erik Welander
  • 83
  • 1
  • 7
6
votes
1 answer

std::function and Signal/Slot system

I'm trying to create a simple Signals/Slots system in C++ without boost but I've got some problems when I try to use it with parameters, here is my code : My Signal class : template class Signal { private: typedef std::function
Dono
  • 522
  • 7
  • 11
6
votes
2 answers

what is a 'valid' std::function?

Here: http://en.cppreference.com/w/cpp/utility/functional/function operator bool is described: "Checks whether the stored callable object is valid". Presumably a default constructed std::function is not valid but is this the only case? Also, how…
dpj
  • 933
  • 1
  • 7
  • 14