std::mem_fn

From cppreference.com
< cpp‎ | utility‎ | functional
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
Function objects
Function wrappers
(C++11)
mem_fn
(C++11)
(C++17)
Bind
(C++11)
(C++11)
(C++11)
Reference wrappers
(C++11)(C++11)
Operator wrappers
Negators
(deprecated)
(deprecated)
(deprecated)
(deprecated)
Searchers
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++17)
(until C++17)(until C++17)(until C++17)(until C++17)
(until C++17)(until C++17)
(until C++17)(until C++17)
 
Defined in header <functional>
template< class R, class T >
/*unspecified*/ mem_fn(R T::* pm);
(1) (since C++11)
template< class R, class T, class... Args >

/*unspecified*/ mem_fn(R (T::* pm)(Args...));
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) &&);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) const &&);
template< class R, class T, class... Args >
/*unspecified*/ mem_fn(R (T::* pm)(Args...) volatile &&);
template< class R, class T, class... Args >

/*unspecified*/ mem_fn(R (T::* pm)(Args...) const volatile &&);
(2) (since C++11)
(until C++14)

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.

The overloads (2) were introduced in C++11 but removed in C++14 as defect #2048

Contents

[edit] Parameters

pm - pointer to member that will be wrapped

[edit] Return value

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

std::mem_fn Return 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

Member function

template<class... Args>
/* see below */ operator()(Args&&... args);

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&&...)>::type.)

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

[edit] Exceptions

(none)

(until C++17)
noexcept specification:  
noexcept
  
(since C++17)

[edit] Example 1

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

#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

[edit] Example 2

Demonstrates the effect of the C++14 changes to the specification of std::mem_fn

#include <iostream>
#include <functional>
 
struct X {
    int x;
 
    int&       easy()      {return x;}
    int&       get()       {return x;}
    const int& get() const {return x;}
};
 
 
int main(void)
{
    auto a = std::mem_fn        (&X::easy); // no problem at all
//  auto b = std::mem_fn<int&  >(&X::get ); // no longer works in C++14
    auto c = std::mem_fn<int&()>(&X::get ); // works with both C++11 and C++14
    auto d = [] (X& x) {return x.get();};   // another approach to overload resolution
 
    X x = {33};
    std::cout << "a() = " << a(x) << '\n';
    std::cout << "c() = " << c(x) << '\n';
    std::cout << "d() = " << d(x) << '\n';
}

Output:

a() = 33
c() = 33
d() = 33

[edit] See also

(C++11)
wraps callable object of any type with specified function call signature
(class template)
(C++11)
binds one or more arguments to a function object
(function template)