From cppreference.com
template<classM,classT>/* unspecified */mem_fn(MT::*pm)noexcept;(since C++11)
(constexpr since C++20)Function template std::mem_fn generates wrapper objects for pointers to members, which can store, copy, and invoke a
. 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 fn of unspecified type that has the following members:
std::mem_fnreturn type
Member types
type definition 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 */);(constexpr since C++20)The expression fn(args) is equivalent to
(pmd,args), where pmd is the
object held by fn, it is of type M T::* and is direct-non-list-initialized with pm.
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 std::mem_fn to store and execute a member function and a member object:
Run this code
#include<functional>#include<iostream>#include<memory>structFoo{voiddisplay_greeting(){std::cout<<"Hello, world.\n";}voiddisplay_number(inti){std::cout<<"number: "<<i<<'\n';}intadd_xy(intx,inty){returndata+x+y;}template<typename...Args>intadd_many(Args...args){returndata+(args+...);}autoadd_them(auto...args)// C++20 required{returndata+(args+...);}intdata=7;};intmain(){autof=Foo{};autogreet=std::mem_fn(&Foo::display_greeting);greet(f);autoprint_num=std::mem_fn(&Foo::display_number);print_num(f,42);autoaccess_data=std::mem_fn(&Foo::data);std::cout<<"data: "<<access_data(f)<<'\n';autoadd_xy=std::mem_fn(&Foo::add_xy);std::cout<<"add_xy: "<<add_xy(f,1,2)<<'\n';autou=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';autoadd_many=std::mem_fn(&Foo::add_many<short,int,long>);std::cout<<"add_many(u, ...): "<<add_many(u,1,2,3)<<'\n';autoadd_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';}Output:
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 Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++11 unnecessary overloads provided removed
C++11 noexcept not required required See also
(C++11)
copyable wrapper of any copy constructible callable object
(class template)
(C++23)
move-only wrapper of any callable object that supports qualifiers in a given call signature
(class template)
(C++11)
binds one or more arguments to a function object
(function template)