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

Using bind inside of function call

Ok so I already have this as a working example but I'm just trying to clean it up. currently I have a subscription styled event system with callback routines. I have no problem with adding regular functions and class member functions into the…
Steven Venham
  • 600
  • 1
  • 3
  • 18
-1
votes
1 answer

how to make a operator have an explicit parameter c++

consider some code: void foo() { } bool bar() { return true; } struct S { void operator=(std::function f){f();}; void operator=(std::function f){f();}; }; int main() { S s; s = foo; // ok s = bar; //…
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
-1
votes
1 answer

C++ how to overload operators for member variables

If I have a struct: typedef struct Foo { std::function func; }Foo; and I have a void function void bar(){std::cout << "hey";} and I want to be able to do this: Foo f; f.func = bar // f.func == func(){std::cout << "hey"; return true;}
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
-1
votes
1 answer

Call wrapper inside shared_ptr<> instance on its member function

I'm trying to make a forwarding call wrapper with std::bind() of an internal member function inside instance which has been created as a shared_ptr<>. Look like there's no chance. In a nutshell: std::shared_ptr
danilabagroff
  • 602
  • 1
  • 7
  • 23
-1
votes
1 answer

Calling one templated function isn't working

I don't know how to call function call. It's a templated class, with a templated function call. But how can I use this code? #include #include #include template class Imp { template
-1
votes
2 answers

Executing bound std::function throws std::bad_function_call

I want to bind a member function to a std::function. I heard that member functions take one extra parameter which is the instance pointer. Therefore I call std::bind(&Class::Function, this, parameter) but when I execute the function…
danijar
  • 32,406
  • 45
  • 166
  • 297
-2
votes
1 answer

How to use a base class template member function in derived?

how to use a base class template function in a derived case? Example: Class A{ protected: template std::vector generate_value(const size_t& generated_points, std::function
mas
  • 13
  • 4
-2
votes
1 answer

An error occurred while using std::function ( error C2064: c++ term does not evaluate to a function taking 0 arguments)

The following error occurred while developing using C++. What could be the cause?
-2
votes
1 answer

std::function inside template class giving object creation error

Getting below error for the code given below. Trying to have 3 std::function inside an template class and then initialize them with different functions from different classes. Why is this error happening? What's wrong with below code? template…
D K
  • 71
  • 6
-2
votes
1 answer

Callback with std::function and arguments

I try to do a router : router.get("/mypath", callback) But I have some issue with std::function)> is from boost beast struct Routes { typedef std::function)>…
Vana
  • 753
  • 3
  • 11
  • 20
-2
votes
1 answer

How to pass a parameter to std::bind from a function

I am working on c++ 11. I want to write a function funA in a class, which binds another function funB within the same class. And funB function is a parameter in the function funA. What could be the syntax of the funcA ? I tried using std::function…
SuryaRao
  • 51
  • 1
  • 6
-2
votes
3 answers

Passing a member function of another class into a std::function parameter

I have a class with a function that takes a std::function and stores it. This part seems to compile ok (but please point out any issue if there are any) #include #include struct worker { std::function
code_fodder
  • 15,263
  • 17
  • 90
  • 167
-2
votes
1 answer

std::function extract and remove argument

I'm learning c++11/14 these days and it seems like a whole new language to me with all the great additions but I still can't quite make use of all these new features: typedef std::function void_f; typedef std::function
Petar
  • 1,034
  • 1
  • 12
  • 18
-3
votes
1 answer

std::function as a std::map key

I want to insert std::function as a std::map key, but the code doesnt compile, any thoughts? int sum(int a, float b){ return a+b; } int main() { std::functionf = sum; std::map ,…
black_gay
  • 143
  • 7
-3
votes
1 answer

Wrap intro std function

So, I have no reason to use std::function for this statement. Just from what I read the following statement can be written using std::function. How should this statement look like? Written with std::function template class…
merry
  • 3
  • 3
1 2 3
56
57