stdbind

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

How do I store a vector of std::bind without a specific case for the template?

寵の児 提交于 2020-02-01 04:02:51
问题 After going though a question on std::bind , I was wondering if it was possible to hold a vector of functions created by std::bind so I can avoid using std::function and its heavyweight wrapping. #include <iostream> #include <functional> #include <typeinfo> #include <vector> int add(int a, int b) {return a + b;} int main() { //I believe this here is just a special type of bound function. auto add2 = std::bind(add, std::placeholders::_1, 2); auto add3 = std::bind(add, std::placeholders::_1, 3)

std::bind(): bind lambda with rvalue reference as argument

空扰寡人 提交于 2020-01-23 03:27:28
问题 I am playing with std::bind and rvalue references, but I still don't figure out how it works, I have the following code: class Dog { public: Dog(const string &name) : name_(name) { cout << "Dog::ctor" << endl; } string GetName() { return name_; } private: string name_; }; auto bind_fun = bind([](Dog &&d){ cout << d.GetName() << endl; }, Dog("DogABC")); bind_fun(); When commenting out bind_fun() , or if the lambda takes Dog& rather than Dog&& , the code run fine with expected output. When bind

How to bind function to an object by reference?

会有一股神秘感。 提交于 2020-01-10 08:59:50
问题 I have the following code to bind a member function to an instance of the class: class Foo { public: int i; void test() { std::cout << i << std::endl; } }; int main() { Foo f; f.i = 100; auto func = std::bind(&Foo::test, std::forward<Foo>(f)); f.i = 1000; func(); } But the std::bind statement does not bind to f by reference. Calling func prints "100" instead of "1000" which is what I want. However, If I change the statement to take a pointer it works. auto func = std::bind(&Foo::test, &f);

How to bind function to an object by reference?

回眸只為那壹抹淺笑 提交于 2020-01-10 08:58:29
问题 I have the following code to bind a member function to an instance of the class: class Foo { public: int i; void test() { std::cout << i << std::endl; } }; int main() { Foo f; f.i = 100; auto func = std::bind(&Foo::test, std::forward<Foo>(f)); f.i = 1000; func(); } But the std::bind statement does not bind to f by reference. Calling func prints "100" instead of "1000" which is what I want. However, If I change the statement to take a pointer it works. auto func = std::bind(&Foo::test, &f);

Alterantive for callbacks using std::function

别来无恙 提交于 2020-01-04 03:59:20
问题 Currently I am trying out a code that does essentially the following: void f(int x) { cout << "f("<<x<<")" << endl; } class C { public: void m(int x) { cout << "C::m("<<x<<")" << endl; } }; class C2 { public: void registerCallback(function<void(int)> f) { v.push_back(f); } private: vector<function<void(int)>> v; void callThem() { for (int i=0; i<v.size(); i++) { v[i](i); } } }; int main() { C2 registrar; C c; registrar.registerCallback(&f); // Static function registrar.registerCallback(bind(

What's the best way to wrap a C callback with a C++11 interface?

♀尐吖头ヾ 提交于 2019-12-30 06:00:09
问题 Let's say this is a C function to be wrapped: void foo(int(__stdcall *callback)()); The two main pitfalls with C function pointer callbacks are: Not being able to store bind expressions Not being able to store capturing lambdas I would like to know the best way to wrap functions like these to do so. The first is particularly useful for a member function callback, and the second for an inline definition that uses surrounding variables, but those are not the only uses. The other property of