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.

705 questions
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
  • 235,777
  • 25
  • 285
  • 465
27
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,684
  • 2
  • 23
  • 36
27
votes
5 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
  • 27,743
  • 34
  • 143
  • 257
24
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
  • 4,594
  • 4
  • 29
  • 34
23
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.
  • 857
  • 3
  • 9
  • 21
23
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
  • 4,877
  • 7
  • 44
  • 66
22
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,296
  • 1
  • 20
  • 34
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
  • 26,080
  • 13
  • 87
  • 116
20
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
  • 1,150
  • 1
  • 14
  • 26
19
votes
3 answers

Using `std::function` to call non-void function

A while ago I used std::function pretty much like this: std::function func = [](int i) -> int { return i; }; Basically, I did this because I wanted to store different function objects in a std::function, but I didn't want to restrict the…
Ken Wayne VanderLinde
  • 17,085
  • 2
  • 42
  • 66
19
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
  • 3,901
  • 7
  • 50
  • 100
19
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
  • 834
  • 4
  • 22
19
votes
7 answers

Binding to a weak_ptr

Is there a way to std::bind to a std::weak_ptr? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed. I know how to create a std::function using a shared_ptr: std::function
Scotty
  • 2,330
  • 1
  • 13
  • 18
18
votes
1 answer

Call operator with auto return type being chosen instead of constructor when using std::function

The following snippet: #include struct X { X(std::function fn); // (1) X(double, double); // (2) template auto operator()(T const& t) const { // (3) return t.foo(); …
Holt
  • 32,271
  • 6
  • 74
  • 116
18
votes
4 answers

how to declare properly the template taking function type as a parameter (like a std::function)

I found out that its not trivial to have a neat syntax like in: std::function If I declare the function as: template class function {}; It would be an ordinary syntax to define function's templated…
barney
  • 1,736
  • 1
  • 10
  • 21
1
2
3
46 47