Namespaces
Variants

std:: mem_fun_ref

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

mem_fun_ref
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
定义于头文件 <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 ( ) 的第一个参数。

1) 有效地调用 std:: mem_fun_ref_t < S,T > ( f ) std:: const_mem_fun_ref_t < S,T > ( f )
2) 有效调用 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 中移除)
从成员函数指针创建包装器,可通过对象指针调用
(函数模板)