简体   繁体   English

可以通过boost获得C ++匿名函数吗?

[英]Possible to have C++ anonymous functions with boost?

I'm trying to solve a problem that anonymous functions make much, much easier, and was wondering if this was possible in c++. 我正在尝试解决匿名函数变得更容易,更容易的问题,并且想知道这是否可以在c ++中实现。

What I would like to do is (essentially) 我想做的是(基本上)

template<typename T>
T DoSomething(T one, function<T(T)> dosomething)
{
    return one + dosomething(5);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

This example is very, very simplified for what I have to do. 对于我必须做的事情,这个例子非常非常简单。 In C# I would do p => p*5. 在C#中我会做p => p * 5。 I know this is easy with C++0x, but I can't use that. 我知道这很容易用C ++ 0x,但我不能用它。 I feel that I should be able to do it with either boost::lambda, or a compination of boost::bind and boost::function with placeholders, but I can't seem to get it to work. 我觉得我应该能够使用boost :: lambda,或使用占位符的boost :: bind和boost :: function的组合,但我似乎无法让它工作。 This may not be possible and thats also fine, but please answer if its not possible. 这可能是不可能的,这也很好,但如果不可能请回答。 Thanks. 谢谢。

EDIT: Ok, it seems the simple case of an int works fine, what about a more complicated structure? 编辑:好吧,似乎int的简单情况工作正常,一个更复杂的结构怎么样? So, lets try 所以,试试吧

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

Here my C# expression would be along the lines of p => p.temp * 5. Is this possible to do in C++ with boost? 这里我的C#表达式将与p => p.temp * 5一致。这是否可以在C ++中使用boost进行?

EDIT 2: OK, now I'm just curious :D How would I call a function within the lambda expression? 编辑2:好的,现在我只是好奇:D如何调用lambda表达式中的函数? So, if we have 所以,如果我们有

int ChangeVal(int mult)
{
    return mult*5;
}

struct NumHolder
{
  int x;
}

template<typename T>
T DoSomething(T one, function<T(NumHolder)> dosomething)
{
    NumHolder temp;
    temp = 5
    return one + dosomething(temp);
}

void GetMyVal(...)
{
   DoSomething<int>(1, /*anonymous func here*/)
}

In C# I could call p => ChangeVal(p). 在C#中,我可以调用p => ChangeVal(p)。 What would the syntax be for this with the C++ lambda expressions? C ++ lambda表达式的语法是什么?

As Anders notes in his answer, boost::lambda can be useful, but the code can become hard to read in some cases. 正如Anders在他的回答中指出的那样,boost :: lambda可能很有用,但在某些情况下代码可能变得难以阅读。 It thus depends on what you want to do in your anonymous function. 因此,它取决于您在匿名函数中要执行的操作。

For simple case like the p => p * 5 you mention in your question, it seems to me that using Lambda or Bind would be reasonable, though: 对于你在问题中提到的p => p * 5这样的简单情况,在我看来,使用Lambda或Bind是合理的,但是:

DoSomething(1, _1 * 5);

Edit: Your second example hits one area where the syntax gets quickly verbose: Member (data or function) access. 编辑:您的第二个示例命中一个语法快速详细的区域:成员(数据或函数)访问。 Because the "dot" operator can't be overloaded in C++, you have to use a bind expression to get the "x" from the argument: 因为“点”运算符不能在C ++中重载,所以必须使用绑定表达式从参数中获取“x”:

DoSomething(1, bind(&NumHolder::x, _1) * 5);

or, with Boost.Lambda, use the overloaded ->* operator: 或者,使用Boost.Lambda,使用重载 - > *运算符:

DoSomething(1, &_1->* &NumHolder::x * 5);

Edit 2: OK, one last time :) In your last question, you write that in C#, you'd write p => ChangeVal(p) , but the code above shows a ChangeVal taking an int, not a NumHolder, so it's not clear what you mean. 编辑2:好的,最后一次:)在你的上一个问题中,你用C#编写,你会写p => ChangeVal(p) ,但上面的代码显示一个ChangeVal采用int,而不是NumHolder,所以它是不清楚你的意思。

Assuming that ChangeVal takes an int and that you want the anonymous function to do the equivalent of ChangeVal(the_arg.x) , you'd write this with Boost.Lambda: 假设ChangeVal接受一个int并且您希望匿名函数执行相当于ChangeVal(the_arg.x) ,那么您可以使用Boost.Lambda编写它:

DoSomething(1, bind(&ChangeVal, &_1->*&NumHolder::x));

or this with Boost.Bind (works with Lambda too): 或者使用Boost.Bind(也适用于Lambda):

DoSomething(1, bind(&ChangeVal, bind(&NumHolder::x, _1));

No, it isn't possible to do in a simple way. 不,不可能以简单的方式做到。 boost::lambda can help, but in my opinion the code is so hard to read when using it so I would avoid it. boost :: lambda可以提供帮助,但在我看来,使用它时代码很难阅读,所以我会避免使用它。

I think the equivalent to C# p=>p*5 would be _1*5 , but I've only looked at it briefly so I'm not sure. 我认为相当于C# p=>p*5将是_1*5 ,但我只是简单地看了它,所以我不确定。 For simple stuff it works, but as soon as you need control structures you will have to use another set of control structures which are functionally based, rather than imperative. 对于简单的东西,它可以工作,但是一旦你需要控制结构,你将不得不使用另一组控制结构,这些控制结构是基于功能的,而不是命令式的。 I found this so different from normal C++ code that I decided for myself that it is not worth using it, because it makes the code so hard to read for others. 我发现这与普通的C ++代码有很大不同,我自己决定不值得使用它,因为它使得代码很难为其他人阅读。

boost doesn't extend syntax of c++. boost不会扩展c ++的语法。 there are no anonymous functions in c++. c ++中没有匿名函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM