C ++中没有的C ++ 0x lambda函数? [英] C++ lambda function without C++0x?

查看:184
本文介绍了C ++中没有的C ++ 0x lambda函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ 11 lambda函数,它需要无C ++ 0x中得以实现。我没有到目前为止成功boost.lambda尝试过。

I have a C++11 lambda function and it needs to be realised without C++0x. I tried it with boost.lambda without success so far.

lambda函数捕获4个变量,并采取2作为参数。在lambda函数体有几个的if / else,病例和大约100 code线。

The lambda function captures 4 variables and takes 2 as parameters. In the body of the lambda function are several if/else, cases and around 100 lines of code.


  • 这是即使boost.lambda可能/这可怎么实现?

  • 会很高兴,如果你能指出一个库/举一个例子。

希望我写的你需要的所有信息。

Hope I wrote all information you need.

€:
我得到了它运行的,谢谢。小跟进的问题:

€: I got it running,thanks. A little follow up question:


  • 是也有办法给Obj.operater()();另一个函数作为callbackFunction参数/函数指针?

我的第一次尝试是这样的:

My first try was like this:

    Lambda Obj( C, D);
    command ( Obj.operator()(typeA A, typeB B));

感谢

推荐答案

这是不是Boost.Lambda是专为。最好的办法是把它变成一个普通函数对象。

This is not what Boost.Lambda is designed for. Your best bet is to turn this into a normal function object.

只要改变这个:

[a, &b, c, &d](args) -> ReturnValue { body; }

进入这个:

class Lambda
{
  Type_of_a a;
  Type_of_b &b;
  Type_of_c c;
  Type_of_d &d;

public:
  Lambda(Type_of_a a, Type_of_b &b, Type_of_c c, Type_of_d &d) : a(a), b(b), c(c), d(d) {}

  ReturnValue operator() (args) const
  { body; }
};

和创造在其中创建原来的lambda前pression的 LAMBDA 类的一个实例。

And create an instance of the Lambda class where you create the original lambda expression.

您可以把 LAMBDA 的定义,一位不愿透露姓名的命名空间,也模拟了拉姆达的未命名的类型。

You can put the definition of Lambda in an unnamed namespace, to simulate the lambda's unnamed type also.

这篇关于C ++中没有的C ++ 0x lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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