Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

mem_fn

类模板  <functional>

template <class Ret, class T>
  /* unspecified */ mem_fn (Ret T::* pm);

将成员函数转换为函数对象
返回一个函数对象,其函数调用援引pm所指向的成员函数。

返回对象的类型有以下属性:


☲  参数


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

☂ 异常安全性



无抛出保证:从不抛出异常。

🍄  另请参阅



ref 构建引用包装器(类模板)
bind 绑定函数参数(类模板)

联系我们 免责声明 关于CandCplus 网站地图