There may be another, simpler, way to do this, but one alternative is using a phoenix function(tested on g++ 4.8.1 without -std=c++11):
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/phoenix.hpp>
using namespace std;
namespace ph = boost::phoenix;
using ph::arg_names::arg1;
bool is_odd(int i) {
return (i % 2 == 1);
}
struct is_odd_impl
{
template <typename Sig>
struct result;
template <typename This, typename Arg>
struct result<This(Arg)>
{
typedef bool type;
};
template <typename Arg>
bool operator()(const Arg& arg) const
{
return is_odd(arg);
//return arg.is_odd();
}
};
ph::function<is_odd_impl> lazy_is_odd = is_odd_impl();
template <typename Predicate>
void foo(vector<int> &v, Predicate p) {
for_each(v.begin(), v.end(),
if_(p(arg1))
[
cout << "found " << arg1 << ", "
]);
cout << endl;
}
int main() {
vector<int> v(10);
int cont = 1;
std::generate(v.begin(),v.end(),ph::ref(cont)++);
cout << "looking for odd ones ";
foo(v, arg1 % 2 == 1);
cout << "using named function ";
foo(v, lazy_is_odd);
}