使用boost创建一个始终返回true的lambda函数

stu*_*ith 5 c++ lambda boost c++03

假设我有一个函数,它采用某种形式的谓词:

void Foo( boost::function<bool(int,int,int)> predicate );
Run Code Online (Sandbox Code Playgroud)

如果我想用一个总是返回true的谓词来调用它,我可以定义一个辅助函数:

bool AlwaysTrue( int, int, int ) { return true; }
...
Foo( boost::bind( AlwaysTrue ) );
Run Code Online (Sandbox Code Playgroud)

但是无论如何调用这个函数(可能使用boost :: lambda)而不必定义一个单独的函数?

[编辑:忘了说:我不能使用C++ 0x]

SCF*_*nch 9

UncleBens在Scharron的回答中评论了这一点,但我认为这实际上是最好的答案所以我偷了它(对不起UncleBens).简单地使用

Foo(boost::lambda::constant(true));
Run Code Online (Sandbox Code Playgroud)

Boost.Lambda的文档所述,只有函子的最小arity为零,最大arity是无限的.因此,传递给仿函数的任何输入都将被忽略.