std-function

Can std::function be used to store a function with variadic arguments [duplicate]

核能气质少年 提交于 2021-02-16 10:09:49
问题 This question already has an answer here : Why can't std::function bind to C-style variadic functions? (1 answer) Closed 6 years ago . I have a structure that I pass around my application which contains a bunch of callback functions: typedef struct { std::function<void (void)> f1; std::function<void (int)> f2; std::function<int (float *)> f3; // ... and so on } CallbackTable; I handle state control within the application by binding different functions to the various callbacks, depending upon

invalid use of void expression in context of c++ std::function

我们两清 提交于 2021-02-07 22:37:08
问题 In below code snippet while calling call back function "Invalid use of void expression" error is flashed by the compiler. #include <iostream> #include <functional> using namespace std; template<class type> class State { public: State(type type1,const std::function<void (type type1 )> Callback) { } }; template <class type> void Callback(type type1 ) { //Based on type validation will be done here } int main() { State<int> obj(10,Callback(10)); return 0; } Just want to know what is the wrong

test std::function validity after bind to member function

别等时光非礼了梦想. 提交于 2021-01-28 06:11:03
问题 For the struct: struct A { int var = 10; void check() { std::cout << "var is " << var << std::endl; } }; Say I create a std::function object of A::check() , using a dynamically allocated a object: auto a_ptr = std::make_unique<A>(); std::function<void()> f = std::bind(&A::check, a_ptr.get()); //...later/elsewhere... f(); It would be nice for safety if I could test f to see if the bound A object is still alive. Is it possible to retrieve that information from f directly? Assume a_ptr is not

C++ lambda capture this vs capture by reference

梦想的初衷 提交于 2020-08-19 01:00:30
问题 If I need to generate a lambda that calls a member function, should I capture by reference or capture 'this'? My understanding is that '&' captures only the variables used, but 'this' captures all member variable. So better to use '&'? class MyClass { public: int mFunc() { // accesses member variables } std::function<int()> get() { //return [this] () { return this->mFunc(); }; // or //return [&] () { return this->mFunc(); }; } private: // member variables } 回答1: For the specific example you

Re-assigning an std::function object while inside its execution

大兔子大兔子 提交于 2020-08-09 05:21:28
问题 I have an std::function object I'm using as a callback to some event. I'm assigning a lambda to this object, within which, I assign the object to a different lambda mid execution. I get a segfault when I do this. Is this not something I'm allowed to do? If so, why? And how would I go about achieving this? declaration: std::function<void(Data *)> doCallback; calling: // // This gets called after a sendDataRequest call returns with data // void onIncomingData(Data *data) { if ( doCallback ) {

How expensive is to copy an std::function?

微笑、不失礼 提交于 2020-08-06 10:33:30
问题 While std::function is movable, in some cases it's not possible or not convenient. Is there a significant penalty for copying it? Does it possibly depend on the size of the captured variables (if it was created using a lambda expression)? Is it implementation dependent? 回答1: It is indeed implementation-dependent. It also depends on what exactly you are storing in the std::function . It is surely not as cheap as simply copying a C-style function pointer. The best thing would be for you to try

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

冷暖自知 提交于 2020-03-17 08:02:38
问题 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 parameters; but they accept function pointers. Why? Here is my code: #include <iostream> #include <functional> /* Does not work:*/ template <typename return_type, typename arg_type, std::function<return_type(arg_type)> call_back> /* Work fine: *///

Lambda of a lambda : the function is not captured

帅比萌擦擦* 提交于 2020-03-17 05:34:50
问题 The following program do not compile : #include <iostream> #include <vector> #include <functional> #include <algorithm> #include <cstdlib> #include <cmath> void asort(std::vector<double>& v, std::function<bool(double, double)> f) { std::sort(v.begin(), v.end(), [](double a, double b){return f(std::abs(a), std::abs(b));}); } int main() { std::vector<double> v({1.2, -1.3, 4.5, 2.3, -10.2, -3.4}); for (unsigned int i = 0; i < v.size(); ++i) { std::cout<<v[i]<<" "; } std::cout<<std::endl; asort(v

Can std::async call std::function objects?

十年热恋 提交于 2020-02-21 12:01:06
问题 Is it possible to call function objects created with std::bind using std::async. The following code fails to compile: #include <iostream> #include <future> #include <functional> using namespace std; class Adder { public: int add(int x, int y) { return x + y; } }; int main(int argc, const char * argv[]) { Adder a; function<int(int, int)> sumFunc = bind(&Adder::add, &a, 1, 2); auto future = async(launch::async, sumFunc); // ERROR HERE cout << future.get(); return 0; } The error is: No matching

Can std::async call std::function objects?

此生再无相见时 提交于 2020-02-21 11:55:35
问题 Is it possible to call function objects created with std::bind using std::async. The following code fails to compile: #include <iostream> #include <future> #include <functional> using namespace std; class Adder { public: int add(int x, int y) { return x + y; } }; int main(int argc, const char * argv[]) { Adder a; function<int(int, int)> sumFunc = bind(&Adder::add, &a, 1, 2); auto future = async(launch::async, sumFunc); // ERROR HERE cout << future.get(); return 0; } The error is: No matching