std::mem_fun_ref
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <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() 的第一个参数。
此函数和相关类型在 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 中移除) |
从成员函数指针创建包装器,可使用对象指针调用 (函数模板) |