Using boost to create a lambda function which always returns true

[亡魂溺海] 提交于 2019-12-05 09:02:10

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));

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.

Here is a quick example :

#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <iostream>

void Foo( boost::function<bool(int,int,int)> predicate )
{
  std::cout << predicate(0, 0, 0) << std::endl;
}

int main()
{
  using namespace boost::lambda;
  Foo(true || (_1 + _2 + _3));
}

The trick is in true || (_1 + _2 + _3) where you are creating a boost lambda with 3 arguments (_1, _2 and _3), always returning true.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!