Lambda表达式作为类中的成员函子 [英] Lambda expression as member functors in a class

查看:215
本文介绍了Lambda表达式作为类中的成员函子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当lambda表达式(LE)成为gcc的一部分,开始一个4.5.1,并希望他们将授予一个方法来摆脱C ++中的那些讨厌的函数指针,这是基本上,我的理解,编译为C函数。所有这些静态声明等...



现在我想在类中使用LE,其中可以选择一个函数的计算方法。但是由于在C ++ 1x的提议中的定义,这似乎是不可能的。这里的代码和问题。



testLE.h



 #include< functional> 
typedef std :: function< double(double,double)> tMyOp;
class testLE
{
public:
testLE(){m_oFactor = 2.5; }
void setOp(const int i)
{
if(i> 0){myOp = plus;} else {myOp = minus;}
}
double eval(double x,double y){return myOp(x,y); }

private:
double m_oFactor;
tMyOp plus;
tMyOp minus;
tMyOp myOp;
};



testLE.cpp



  #includetestLE.h 

tMyOp testLE :: plus = [](double x,double y) - > double
{
return m_oFactor *(x + y);
};

tMyOp testLE :: minus = [](double x,double y) - > double
{
return m_oFactor *(x - y);
};

问题是,不会编译,除非我声明函子_myOp,_minus和_plus为静态,但是一旦我这样做,我不再访问成员变量(在这种情况下,因子),并使用[这]而不是[在函子的定义也不工作。



老实说,imho这比函数指针替代....比较好,所以我会很高兴帮助,但是阅读新标准中LE的规格并没有给出很大的希望。



感谢和最美好的祝愿,
Andy



定义setOp喜欢这个帮助?

/ p>

  void testLE :: setOp(int i)
{
if(i> 0)
myOp = [this](double x,double y) - > double {return m_oFactor *(x + y); };
else
myOp = [this](double x,double y) - > double {return m_oFactor *(x-y); };
}

或者您可以指定 plus

  testLE():: c>和减去 testLE()
{
m_oFactor = 2.5;
plus = [this](double x,double y) - > double {return m_oFactor *(x + y); };
minus = [this](double x,double y) - > double {return m_oFactor *(x-y); };
}


I was thrilled when lambda expressions (LE) were part of the gcc starting a 4.5.1 and hoped they would grant a way of getting rid of those nasty functions pointer in C++, which were basically, to my understanding, compiled as C functions. All those static declarations etc...

Now I wanted to use LEs in a class, where one can choose a method of computation by a functor. But due to the definition in the proposal for C++1x, this seems not to be possible at all. Here the code and the problem(s).

testLE.h

#include<functional>
typedef std::function<double(double, double)> tMyOp;
class testLE
{
  public:
  testLE(){ m_oFactor = 2.5; }
  void setOp(const int i)
  {
    if (i > 0) {myOp = plus;} else {myOp = minus;}
  }
  double eval(double x, double y) { return myOp(x, y); }

private:
  double m_oFactor;
  tMyOp plus;
  tMyOp minus;
  tMyOp myOp;
};

testLE.cpp

#include "testLE.h

tMyOp testLE::plus = [](double x, double y) -> double
{
  return m_oFactor*(x + y);
};

tMyOp testLE::minus = [](double x, double y) -> double
{
  return m_oFactor*(x - y);
};

So the problem is, that this will not compile unless I declare the functors _myOp, _minus and _plus as static, but as soon as I do this, I have no access any longer to the member variables (in this case factor). And using [this] instead of [] in the functors' definition does not work either.

Honestly, imho this is worse than the function pointer alternative.... So I would be very glad about help, but reading the specs for LEs in the new standard does not give much hope.

Thanks and best wishes, Andy

解决方案

I find it not entirely clear what you want to do.

Would defining setOp like this help?

void testLE::setOp(int i)
{
    if (i > 0) 
        myOp = [this](double x, double y) -> double { return m_oFactor*(x + y); };
    else
        myOp = [this](double x, double y) -> double { return m_oFactor*(x - y); };
}

Or you can assign plus and minus in the constructor:

testLE()::testLE()
{
    m_oFactor = 2.5;
    plus = [this](double x, double y) -> double { return m_oFactor*(x + y); };
    minus = [this](double x, double y) -> double { return m_oFactor*(x - y); };
}

这篇关于Lambda表达式作为类中的成员函子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆