Lambda 闭包作为C++;

Lambda 闭包作为C++;,lambda,callback,closures,c++17,using,Lambda,Callback,Closures,C++17,Using,我很难实现一个类方法,该类方法返回一个捕获this(在示例Foo::f1中)的闭包。其思想是将其用作栏中的回调 第一个障碍是我没有弄清楚如何使用语句在中指定闭包类型 #include <iostream> class Foo { public: Foo() = default; ~Foo() = default; // don't know what the correct type specification is //using fptr = [](int)

我很难实现一个类方法,该类方法返回一个捕获
this
(在示例
Foo::f1
中)的闭包。其思想是将其用作
栏中的回调

第一个障碍是我没有弄清楚如何使用
语句在
中指定闭包类型

#include <iostream>

class Foo {
 public:
  Foo() = default;
  ~Foo() = default;

  // don't know what the correct type specification is
  //using fptr = [](int)->int;
  //using fptr = int operator()(int);
  //using fptr = int (*)();
  typedef int (*fptr)(int);

  fptr f1(void) {
    return [this](int k)->int { return k * this->x_; };
  }

 private:
  int x_ = 2;
};

class Bar {
 public:
  Bar() = default;
  ~Bar() = default;
  void setfun(Foo::fptr f) { f_ =  f; }
  void callfun() {
    std::cout << "result = " << f_(8) << std::endl;
  }
 private:
  Foo::fptr f_;
};


int main(int, char **) {
  Foo foo;
  Bar bar;
  bar.setfun(foo.f1());
  bar.callfun();
  return 0;
}
#包括
福班{
公众:
Foo()=默认值;
~Foo()=默认值;
//不知道正确的型号规格是什么
//使用fptr=[](int)->int;
//使用fptr=int运算符()(int);
//使用fptr=int(*)();
类型定义内部(*fptr)(内部);
fptr f1(无效){
return[this](intk)->int{returnk*this->x_;};
}
私人:
int x_uu=2;
};
分类栏{
公众:
Bar()=默认值;
~Bar()=默认值;
void setfun(Foo::fptr f){f_u=f;}
void callfun(){

std::cout标记
f1
功能为返回
自动

  auto f1(void) 
  {
    return [this](int k)->int { return k * this->x_; };
  }
然后,您可以从
类模板,以及在安装时 使用
decltype
获取
f1
的返回类型:

template<class F>
class Bar {
};

Foo foo;
Bar< decltype( std::declval<Foo>().f1() ) > bar;
完整代码为:

class Foo {
 public:
  Foo() = default;
  ~Foo() = default;

  auto f1(void) 
  {
    return [this](int k)->int { return k * this->x_; };
  }

 private:
  int x_ = 2;
};

template<class F>
class Bar 
{
 public:
  Bar() = default;
  ~Bar() = default;

   void setfun(F f) { f_ =  std::make_unique<F>(f); }

   void callfun() 
   {
     std::cout << "result = " << (*f_)(8) << std::endl;
   }
 private:
  std::unique_ptr<F> f_;
};


int main(int, char **)  {
  Foo foo;
  Bar< decltype( std::declval<Foo>().f1() ) > bar;

  bar.setfun(foo.f1());
  bar.callfun();
class-Foo{
公众:
Foo()=默认值;
~Foo()=默认值;
自动f1(无效)
{
return[this](intk)->int{returnk*this->x_;};
}
私人:
int x_uu=2;
};
模板
分类栏
{
公众:
Bar()=默认值;
~Bar()=默认值;
void setfun(F){F_u=std::make_unique(F);}
void callfun()
{

这能回答你的问题吗?
class Foo {
 public:
  Foo() = default;
  ~Foo() = default;

  auto f1(void) 
  {
    return [this](int k)->int { return k * this->x_; };
  }

 private:
  int x_ = 2;
};

template<class F>
class Bar 
{
 public:
  Bar() = default;
  ~Bar() = default;

   void setfun(F f) { f_ =  std::make_unique<F>(f); }

   void callfun() 
   {
     std::cout << "result = " << (*f_)(8) << std::endl;
   }
 private:
  std::unique_ptr<F> f_;
};


int main(int, char **)  {
  Foo foo;
  Bar< decltype( std::declval<Foo>().f1() ) > bar;

  bar.setfun(foo.f1());
  bar.callfun();