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
35
votes
6 answers

How to directly bind a member function to an std::function in Visual Studio 11?

I can easily bind member functions to a std::function by wrapping them with a lambda expression with capture clause. class Class { Class() { Register([=](int n){ Function(n); }); } void Register(std::function
danijar
  • 32,406
  • 45
  • 166
  • 297
34
votes
3 answers

How are C++11 lambdas represented and passed?

Passing a lambda is really easy in c++11: func( []( int arg ) { // code } ) ; But I'm wondering, what is the cost of passing a lambda to a function like this? What if func passes the lambda to other functions? void func( function< void (int arg)…
bobobobo
  • 64,917
  • 62
  • 258
  • 363
31
votes
1 answer

The reasoning behind Clang's implementation of std::function's move semantics

In libc++'s implementation of std::function, if the function object whose type is being erased is small enough to fit inside an SBO then the move operation will copy it, not move it. Yet not every object whose stack memory footprint is small is…
GreenScape
  • 7,191
  • 2
  • 34
  • 64
30
votes
2 answers

C++ function types

I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function): typedef int Signature(int); // the signature in question typedef std::function std_fun_1; typedef…
marton78
  • 3,899
  • 2
  • 27
  • 38
29
votes
3 answers

What is the return type of a lambda expression if an item of a vector is returned?

Consider the following snippet: #include #include #include int main() { std::vectorv = {0,1,2,3,4,5,6}; std::function f = [&v](int i) { return v[i];}; std::function
Jing Li
  • 639
  • 7
  • 18
28
votes
4 answers

C++11 variadic std::function parameter

A function named test takes std::function<> as its parameter. template void test(std::function f) { // ... } But, if I do the following: void foo(int n) { /* ... */ } // ... test(foo); Compiler(gcc 4.6.1)…
Daniel K.
  • 947
  • 3
  • 11
  • 21
28
votes
5 answers

Using 'void' template arguments in C++

Take the following minimal example: using Type1 = std::function; template using Type2 = std::function; Type1 whyDoesThisWork; Type2 andYetThisDoesNot; If the second type alias, I get the error "Argument may…
Nick Hutchinson
  • 5,044
  • 5
  • 33
  • 34
27
votes
1 answer

Is it illegal to invoke a std::function under the standard?

All quotes are from N3797. 4/3 [conv] An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t This implies no expression can be…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
26
votes
2 answers

Functor reference through a std::function

Basically, I would like to have the following semantic : #include #include class test { public: void add(std::function f) { f(); } void operator()() { ++x; } int x =…
Julio Guerra
  • 5,523
  • 9
  • 51
  • 75
25
votes
2 answers

Why isn't std::function a valid template parameter while a function pointer is?

I have defined class template named CallBackAtInit which only purpose is to call a function at its initialization (constructor). The function is specified in template parameters. The problem is that templates does not accept std::function as…
Leonardo Raele
  • 2,400
  • 2
  • 28
  • 32
24
votes
6 answers

Get the name of a std::function

In the following toy-example, I would like to get the name of a function. The function itself was given as an std::function argument. Is it possible in C++ to get name of a std::function object? void printName(std::function func){ //Need…
hr0m
  • 2,643
  • 5
  • 28
  • 39
24
votes
2 answers

Can I use named parameters in std::function template signature type argument?

Can I legally use names for template parameters in std::function (or another similar construct)? E.g. given the code std::function some_func; Can I rewrite it in following way? std::function some_func; It…
ciechowoj
  • 914
  • 6
  • 26
22
votes
9 answers

Vector of std::function with different signatures

I have a number of callback functions with different signatures. Ideally, I would like to put these in a vector and call the appropriate one depending on certain conditions. e.g. void func1(const std::string& value); void func2(const std::string&…
ksl
  • 4,519
  • 11
  • 65
  • 106
22
votes
4 answers

How should I define a std::function variable with default arguments?

To set a std::function variable to a lambda function with default argument I can use auto as in: auto foo = [](int x = 10){cout << x << endl;}; foo(); This will print 10. But I want the foo variable to reside in a struct. In a struct I cannot use…
wibek
  • 355
  • 1
  • 2
  • 8
22
votes
2 answers

Have the ideas behind the Fast Delegate (et al) been used to optimize std::function?

There have been proposals for C++ "delegates" which have lower overhead than boost::function: Member Function Pointers and the Fastest Possible C++ Delegates Fast C++ Delegate The Impossibly Fast C++ Delegates Have any of those ideas been used to…
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
1
2
3
56 57