使用boost创建一个lambda函数,它总是返回true [英] Using boost to create a lambda function which always returns true

查看:293
本文介绍了使用boost创建一个lambda函数,它总是返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个函数,它接受某种形式的predicate的:

Suppose I have a function which takes some form of predicate:

void Foo( boost::function<bool(int,int,int)> predicate );

如果我想用predicate总是返回true称呼它,我可以定义一个辅助函数:

If I want to call it with a predicate that always returns true, I can define a helper function:

bool AlwaysTrue( int, int, int ) { return true; }
...
Foo( boost::bind( AlwaysTrue ) );

不过,反正是有,而无需定义一个单独的函数来调用这个函数(可能使用boost ::拉姆达)?

But is there anyway to call this function (possibly using boost::lambda) without having to define a separate function?

推荐答案

在UncleBens Scharron的回答这个评论,但我认为它实际上是最好的答案,所以我偷它(抱歉UncleBens)。只需使用

UncleBens commented on this in Scharron's answer, but I think it is actually the best answer so I'm stealing it (sorry UncleBens). Simply use

Foo(boost::lambda::constant(true));

为Boost.Lambda ,只有算符的最小元数是零,最大元数是无限的。所以传递到仿函数的任何输入将完全忽略。

As described in the documentation for Boost.Lambda, only the minimum arity of the functor is zero, the maximum arity is unlimited. So any inputs passed to the functor will simply be ignored.

这篇关于使用boost创建一个lambda函数,它总是返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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