std:: mem_fun
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<functional>
|
||
|
template
<
class
Res,
class
T
>
std:: mem_fun_t < Res,T > mem_fun ( Res ( T :: * f ) ( ) ) ; |
(1) |
(C++11 中弃用)
(C++17 中移除) |
|
template
<
class
Res,
class
T
>
std:: const_mem_fun_t < Res,T > mem_fun ( Res ( T :: * f ) ( ) const ) ; |
(1) |
(C++11 中弃用)
(C++17 中移除) |
|
template
<
class
Res,
class
T,
class
Arg
>
std:: mem_fun1_t < Res,T,Arg > mem_fun ( Res ( T :: * f ) ( Arg ) ) ; |
(2) |
(C++11 中弃用)
(C++17 中移除) |
|
template
<
class
Res,
class
T,
class
Arg
>
std:: const_mem_fun1_t < Res,T,Arg > mem_fun ( Res ( T :: * f ) ( Arg ) const ) ; |
(2) |
(C++11 中弃用)
(C++17 中移除) |
创建一个成员函数包装器对象,通过模板参数推导目标类型。该包装器对象期望在其
operator
(
)
的第一个参数中接收一个指向
T
类型对象的指针。
此函数及相关类型在 C++11 中被弃用,并在 C++17 中被移除,转而采用更通用的 std::mem_fn 和 std::bind ,这两者都能从成员函数创建可调用适配器兼容的函数对象。
目录 |
参数
| f | - | 指向需要创建包装器的成员函数的指针 |
返回值
一个包装 f 的函数对象。
异常
可能抛出实现定义的异常。
注释
std :: mem_fun 与 std:: mem_fun_ref 的区别在于:前者生成的函数包装器期望接收对象指针,而后者则期望接收对象引用。
示例
演示
std::mem_fun
的用法并与
std::mem_fn
进行比较。可能需要 C++11/14 兼容编译模式:g++/clang++ 使用 -std=c++11,cl 使用 /std:c++11 等。在较新的编译器上(例如 gcc-12),如果未在 C++98 模式下编译,可能会发出"已弃用声明"警告。
#include <functional> #include <iostream> struct S { int get_data() const { return data; } void no_args() const { std::cout << "void S::no_args() const\n"; } void one_arg(int) { std::cout << "void S::one_arg()\n"; } void two_args(int, int) { std::cout << "void S::two_args(int, int)\n"; } #if __cplusplus > 201100 int data{42}; #else int data; S() : data(42) {} #endif }; int main() { S s; std::const_mem_fun_t<int, S> p = std::mem_fun(&S::get_data); std::cout << "s.get_data(): " << p(&s) << '\n'; std::const_mem_fun_t<void, S> p0 = std::mem_fun(&S::no_args); p0(&s); std::mem_fun1_t<void, S, int> p1 = std::mem_fun(&S::one_arg); p1(&s, 1); #if __cplusplus > 201100 // auto p2 = std::mem_fun(&S::two_args); // 错误:mem_fun 仅支持无参数或只有一个参数的成员函数 // 因此,std::mem_fn 是更好的替代方案: auto p2 = std::mem_fn(&S::two_args); p2(s, 1, 2); // auto pd = std::mem_fun(&S::data); // 错误:不支持指向数据成员的指针 // 请改用 std::mem_fn: auto pd = std::mem_fn(&S::data); std::cout << "s.data = " << pd(s) << '\n'; #endif }
可能的输出:
s.get_data(): 42 void S::no_args() const void S::one_arg(int) void S::two_args(int, int) s.data = 42
参见
|
(C++11)
|
从成员指针创建函数对象
(函数模板) |
|
(C++11 中弃用)
(C++17 中移除)
|
创建成员函数指针的包装器,可通过对象引用调用
(函数模板) |