////
Copyright 2024 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
https://www.boost.org/LICENSE_1_0.txt
////

[#mem_fn]
# <boost/compat/mem_fn.hpp>
:idprefix: ref_mem_fn_

## Description

The header `<boost/compat/mem_fn.hpp>` implements the {cpp}11 function
`std::mem_fn`.

`mem_fn(pm)`, where `pm` is a pointer to member, returns a function object
that can be used to invoke the member function, or obtain a reference to the
data member, using a function call syntax.

Even though `std::mem_fn` is {cpp}11, later standards place stricter
requirements on the returned function object (it needs to be SFINAE friendly
and propagate `noexcept` correctly.) `boost::compat::mem_fn` implements
these stricter requirements.

## Example

```
struct X
{
    void f(int a, int b) const noexcept;
};

int main()
{
    auto fn = boost::compat::mem_fn(&X::f);

    X x;
    fn(x, 1, 2); // calls x.f(1, 2)
}
```

## Synopsis

```
namespace boost
{
namespace compat
{

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

} // namespace compat
} // namespace boost
```

## mem_fn

```
template<class M, class T> auto mem_fn(M T::* pm) noexcept;
```

[horizontal]
Returns:;; A function object `fn` such that:
  * `fn(x, a...)` is equivalent to `(x.*pm)(a...)`, when `M` is a function type and the type of `X` is `T` or derived from `T`;
  * `fn(x, a...)` is equivalent to `((*x).*pm)(a...)`, when `M` is a function type and the type of `X` is not `T` or derived from `T`;
  * `fn(x)` is equivalent to `x.*pm`, when `M` is an object type and the type of `X` is `T` or derived from `T`;
  * `fn(x)` is equivalent to `(*x).*pm`, when `M` is an object type and the type of `X` is not `T` or derived from `T`.
