std::
mem_fn
类模板 <functional>
template <class Ret, class T>
/* unspecified */ mem_fn (Ret T::* pm);
|
将成员函数转换为函数对象
返回一个函数对象,其函数调用援引pm所指向的成员函数。
返回对象的类型有以下属性:
-
它的函数调用的第一个参数是一个T类型的对象(或一个引用或指向它的指针),
作为附加参数,是pm所带的参数(如果有的话)。
这种以fn作为第一个参数的调用的效果与调用 fn.*pm (or (*fn).*pm,如果fn是一个指针)相同,传递任何附加参数。
-
它有一个成员result_type,定义为Ret的别名(这是pm的返回类型)。
-
如果pm指向的成员不带参数,它有一个成员argument_type,定义为T*的别名。
-
如果pm指向的成员接受一个参数,它有一个成员first_argument_type,定义为T*的别名,
还有一个成员second_argument_type,定义为pm接受的参数的别名。
-
它是 nothrow move-constructible, nothrow copy-constructible 和 nothrow copy-assignable.
☲ 参数
-
pm
-
成员函数的指针。
☉ 返回值
一个函数对象,当被调用时,调用pm作为第一个参数传递的对象。
返回类型未指定,但返回的类型是具有上述属性的函数对象类。
☣ 示例
// mem_fn example
#include <iostream> // std::cout
#include <functional> // std::mem_fn
struct int_holder {
int value;
int triple() {return value*3;}
};
int main () {
int_holder five {5};
// call member directly:
std::cout << five.triple() << '\n';
// same as above using a mem_fn:
auto triple = std::mem_fn (&int_holder::triple);
std::cout << triple(five) << '\n';
return 0;
}
|
输出:
15
15
☂ 异常安全性
无抛出保证:从不抛出异常。
🍄 另请参阅