std::mem_fn

std::mem_fn

 

C++

 

Utilities library

 

Function objects

 

Defined in header <functional>

  

template< class M, class T >
/*unspecified*/ mem_fn(M T::* pm) noexcept;

 (since C++11)
(until C++20)

template< class M, class T >
constexpr /*unspecified*/ mem_fn(M T::* pm) noexcept;

 (since C++20)
   

Function template std::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke a pointer to member. Both references and pointers (including smart pointers) to an object can be used when invoking a std::mem_fn.

Parameters

pm-pointer to member that will be wrapped

Return value

std::mem_fn returns a call wrapper of unspecified type that has the following members:

std::mem_fn return type

Member types

typedefinition
result_type(deprecated in C++17)the return type of pm if pm is a pointer to member function, not defined for pointer to member object
argument_type(deprecated in C++17)T*, possibly cv-qualified, if pm is a pointer to member function taking no arguments
first_argument_type(deprecated in C++17)T* if pm is a pointer to member function taking one argument
second_argument_type(deprecated in C++17)T1 if pm is a pointer to member function taking one argument of type T1
(until C++20)

Member function

template<class... Args>

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

    noexcept(/* see below */);
 (until C++20)
template<class... Args>

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

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

Let fn be the call wrapper returned by a call to std::mem_fn with a pointer to member pm. Then the expression fn(t, a2, ..., aN) is equivalent to INVOKE(pm, t, a2, ..., aN), where INVOKE is the operation defined in Callable.

Thus, the return type of operator() is std::result_of<decltype(pm)(Args&&...)>::typeor equivalently std::invoke_result_t<decltype(pm), Args&&...>, and the value in noexcept specifier is equal to std::is_nothrow_invocable_v<decltype(pm), Args&&...>) (since C++17).

Each argument in args is perfectly forwarded, as if by std::forward<Args>(args)....

Example

Use mem_fn to store and execute a member function and a member object:

Run this code

#include <functional>
#include <iostream>
 
struct Foo {
    void display_greeting() {
        std::cout << "Hello, world.\n";
    }
    void display_number(int i) {
        std::cout << "number: " << i << '\n';
    }
    int data = 7;
};
 
int main() {
    Foo f;
 
    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';
}

Output:

Hello, world.
number: 42
data: 7

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DRApplied toBehavior as publishedCorrect behavior
LWG 2048C++11unnecessary overloads providedremoved
LWG 2489C++11noexcept not requiredrequired

See also

function

(C++11)

wraps callable object of any type with specified function call signature
(class template)

bind

(C++11)

binds one or more arguments to a function object
(function template)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值