The function template mem_fn creates an unspecified callable wrapper [cw] which calls through the callable target (provided as argument).It provides a big improvement over the std::mem_fun and std::mem_fun_ref, in that at the instantiation point it's not required to decide which argument the callable wrapper will be passed: an object, a pointer or a shared pointer. #include <tr1/functional>
template <class R, class Ty> unspecified mem_fn(R Ty::*pm);
struct object { int method() { return 0; }
int method_int() { return 0; }
int method_int(int n) { return n; }
int method_cv() { return 1; }
int method_cv() const { return 2; }
};
mem_fn(&object::method)(obj); mem_fn(&object::method)(ptr);
mem_fn(static_cast<int(object::*)()>(&object::method_int))(obj); mem_fn(static_cast<int(object::*)(int)>(&object::method_int))(obj,1); mem_fn(static_cast<int(object::*)()>(&object::method_cv))(obj); mem_fn(static_cast<int(object::*)() const>(&object::method_cv))(obj); note: the callable wrappers are highlighted.std::for_each(vec.begin(), vec.end(), mem_fn(&object::method)); // that is equivalent to... // std::for_each(vec.begin(), vec.end(), bind(&object::method, _1)); std::for_each(vec.begin(), vec.end(), bind(mem_fn(static_cast<int(object::*)(int)>(&object::method_int),_1,0xcafebabe));
|
|