std::mem_fun_ref
| 在标头 <functional>
|
||
template< class Res, class T >
std::mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() );
|
中定义 (1) | (C++11 中弃用) (C++17 中移除) |
template< class Res, class T >
std::const_mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() const );
|
(1) | (C++11 中弃用) (C++17 中移除) |
template< class Res, class T, class Arg >
std::mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) );
|
(2) | (C++11 中弃用) (C++17 中移除) |
template< class Res, class T, class Arg >
std::const_mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) const );
|
(2) | (C++11 中弃用) (C++17 中移除) |
创建一个成员函数包装器对象,从模板实参推导出目标类型。该包装器对象要求对其 T 调用运算符的第一个形参是类型为 operator() 对象的引用。
std::mem_fun_ref_t<S,T>(f) 或 std::const_mem_fun_ref_t<S,T>(f)。std::mem_fun1_ref_t<S,T>(f) 或 std::const_mem_fun1_ref_t<S,T>(f)。此函数及相关类型在 C++11 中被弃用,并在 C++17 中被移除,推荐使用更通用的 std::mem_fn 和 std::bind,两者均能从成员函数创建可调用且与适配器兼容的函数对象。
参数
| f | - | 指向需要创建包装器的成员函数的指针 |
返回值
一个包装 f 的函数对象。
异常
可能抛出实现定义的异常。
注释
std:: mem_fun 与 std :: mem_fun_ref 的区别在于:前者生成的函数包装器期望接收对象指针,而后者则期望接收对象引用。
示例
使用
std::mem_fun_ref
绑定
std::string
的成员函数
size()
。
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <string> #include <vector> int main() { std::vector<std::string> v = {"once", "upon", "a", "time"}; std::transform(v.cbegin(), v.cend(), std::ostream_iterator<std::size_t>(std::cout, " "), std::mem_fun_ref(&std::string::size)); }
输出:
4 4 1 4
参见
|
(C++11 中弃用)
(C++17 中移除)
|
从成员函数指针创建包装器,可通过对象指针调用
(函数模板) |