bind

#include <functional>
#include <iostream>
#include <memory>
void f(int n1, int n2, int n3, const int &n4, int n5) {
  std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << ' ' << n5 << '\n';
}
int g(int n1) { return n1; }
struct Foo {
  void print_sum(int n1, int n2) { std::cout << n1 + n2 << '\n'; }
  int data = 10;
};
int main() {
  using namespace std::placeholders; // for _1, _2, _3...
  // demonstrates argument reordering and pass-by-reference
  int n = 7;
  // (_1 and _2 are from std::placeholders, and represent future
  // arguments that will be passed to f1)
  auto f1 = std::bind(f, _2, _1, 42, std::cref(n), n);
  n = 10;
  f1(1, 2, 1001); // 1 is bound by _1, 2 is bound by _2, 1001 is unused
  // makes a call to f(2, 1, 42, n, 7)
  // nested bind subexpressions share the placeholders
  auto f2 = std::bind(f, _3, std::bind(g, _3), _3, 4, 5);
  f2(10, 11, 12); // makes a call to f(12, g(12), 12, 4, 5);
  // bind to a pointer to member function
  Foo foo;
  auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
  f3(5);
  // bind to a pointer to data member
  auto f4 = std::bind(&Foo::data, _1);
  std::cout << f4(foo) << '\n';
}
#include <iostream>
#include <vector>
#include <string>

#include <memory>
using namespace std;
class A
{
public:
    void operator()()
    {
        cout << "lala" << endl;
    }
};
int main()
{
    A    a;
    auto f = bind( a );
    f();
}
//输出lala

参考https://blog.csdn.net/caoshangpa/article/details/79173351

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容