In C++11, you can use std::bind
; it just isn't as obvious how to use it:
#include <functional>
using namespace std::placeholders;
std::find_if(
foo.begin(),
foo.end(),
// create a unary function object that invokes greater<int>::operator()
// with the single parameter passed as the first argument and `bar`
// passed as the second argument
std::bind(std::greater<int>(), _1, bar)
) - foo().begin() - 1;
The key is the use of the placeholder argument, which are declared in the std::placeholders
namespace. std::bind
returns a function object that takes some number of parameters when it is invoked. The placeholders used inside the call to std::bind
show how the arguments provided when the resulting object is invoked map to the argument list to the callable that you're binding to. So, for instance:
auto op1 = std::bind(std::greater<int>(), _1, bar);
op1(5); // equivalent to std::greater<int>()(5, bar)
auto op2 = std::bind(std::greater<int>(), bar, _1);
op2(5); // equivalent to std::greater<int>()(bar, 5)
auto op3 = std::bind(std::greater<int>(), _2, _1);
op3(5, bar); // equivalent to std::greater<int>()(bar, 5)
auto op4 = std::bind(std::greater<int>(), _1, _2);
op4(5, bar); // equivalent to std::greater<int>()(5, bar)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…