命名空间
变体
操作

std::mem_fn

来自 cppreference.cn
< cpp‎ | utility‎ | functional
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式绑定器和适配器
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定义于头文件 <functional>
template< class M, class T >
/* unspecified */ mem_fn( M T::* pm ) noexcept;
(since C++11)
(constexpr since C++20)

函数模板 std::mem_fn 为指向成员的指针生成包装器对象,这些对象可以存储、复制和调用指向成员的指针。当调用 std::mem_fn 时,可以使用对象的引用和指针(包括智能指针)。

内容

[编辑] 参数

pm - 将被包装的指向成员的指针

[编辑] 返回值

std::mem_fn 返回一个未指定类型的调用包装器 fn,它具有以下成员

std::mem_fn 返回类型

成员类型

类型 定义
result_type(在 C++17 中已弃用) 如果 pm 是指向成员函数的指针,则为 pm 的返回类型,对于指向成员对象的指针未定义
argument_type(在 C++17 中已弃用) 如果 pm 是指向不接受参数的成员函数的指针,则为 T*,可能带有 cv 限定符
first_argument_type(在 C++17 中已弃用) 如果 pm 是指向接受一个参数的成员函数的指针,则为 T*
second_argument_type(在 C++17 中已弃用) 如果 pm 是指向接受一个类型为 T1 的参数的成员函数的指针,则为 T1
(直到 C++20)

成员函数

template< class... Args >

/* see below */ operator()(Args&&... args) /* cvref-qualifiers */

    noexcept(/* see below */);
(constexpr since C++20)

表达式 fn(args) 等价于 INVOKE(pmd, args),其中 pmd可调用 对象,由 fn 持有,它的类型是 M T::* 并且使用 pm 直接非列表初始化。

因此,operator() 的返回类型是 std::result_of<decltype(pm)(Args&&...)>::type 或等效地 std::invoke_result_t<decltype(pm), Args&&...>,并且 noexcept 说明符中的值等于 std::is_nothrow_invocable_v<decltype(pm), Args&&...>)(自 C++17 起)

如同通过 std::forward<Args>(args)... 一样,args 中的每个参数都被完美转发。

[编辑] 示例

使用 std::mem_fn 存储和执行成员函数和成员对象

#include <functional>
#include <iostream>
#include <memory>
 
struct Foo
{
    void display_greeting()
    {
        std::cout << "Hello, world.\n";
    }
 
    void display_number(int i)
    {
        std::cout << "number: " << i << '\n';
    }
 
    int add_xy(int x, int y)
    {
        return data + x + y;
    }
 
    template<typename... Args> int add_many(Args... args)
    {
        return data + (args + ...);
    }
 
    auto add_them(auto... args) // C++20 required
    {
        return data + (args + ...);
    }
 
    int data = 7;
};
 
int main()
{
    auto f = Foo{};
 
    auto greet = std::mem_fn(&Foo::display_greeting);
    greet(f);
 
    auto print_num = std::mem_fn(&Foo::display_number);
    print_num(f, 42);
 
    auto access_data = std::mem_fn(&Foo::data);
    std::cout << "data: " << access_data(f) << '\n';
 
    auto add_xy = std::mem_fn(&Foo::add_xy);
    std::cout << "add_xy: " << add_xy(f, 1, 2) << '\n';
 
    auto u = std::make_unique<Foo>();
    std::cout << "access_data(u): " << access_data(u) << '\n';
    std::cout << "add_xy(u, 1, 2): " << add_xy(u, 1, 2) << '\n';
 
    auto add_many = std::mem_fn(&Foo::add_many<short, int, long>);
    std::cout << "add_many(u, ...): " << add_many(u, 1, 2, 3) << '\n';
 
    auto add_them = std::mem_fn(&Foo::add_them<short, int, float, double>);
    std::cout << "add_them(u, ...): " << add_them(u, 5, 7, 10.0f, 13.0) << '\n';
}

输出

Hello, world.
number: 42
data: 7
add_xy: 10
access_data(u): 7
add_xy(u, 1, 2): 10
add_many(u, ...): 13
add_them(u, ...): 42

[编辑] 缺陷报告

以下行为变更的缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 2048 C++11 提供了不必要的重载 已移除
LWG 2489 C++11 noexcept 不是必需的 必需的

[编辑] 参见

(C++11)
任何可复制构造的可调用对象的 copyable 包装器
(类模板) [编辑]
任何支持给定调用签名中限定符的可调用对象的 move-only 包装器
(类模板) [编辑]
(C++11)
将一个或多个参数绑定到函数对象
(函数模板) [编辑]