std::mem_fun(3)
π std::mem_fun π
okay bestie so this is a page from the C++ Standard Library manual (yes, that C++), and we're about to get into std::mem_fun β a true vintage icon that has been through some things. Sit down, hydrate, let's go π«Ά
β¨ NAME β¨
std::mem_fun β literally just std::mem_fun, no cap, that's the whole name πΈ
β¨ Synopsis β¨
Defined in header <functional>:
template< class Res, class T > (1)
(deprecated in C++11)
std::mem_fun_t<Res,T> mem_fun( Res (T::*f)() );
(removed in C++17)
template< class Res, class T >
(deprecated in C++11)
std::const_mem_fun_t<Res,T> mem_fun( Res (T::*f)() const (1)
(removed in C++17)
);
template< class Res, class T, class Arg > (2)
(deprecated in C++11)
std::mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) );
(removed in C++17)
template< class Res, class T, class Arg >
(deprecated in C++11)
std::const_mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) (2)
(removed in C++17)
const );
So what's the vibe? It creates a member function wrapper object, and it deduces the target type from the template arguments (smart, love that for it π). The wrapper object expects a pointer to an object of type T as the first parameter to its operator().
Breaking it down, because we're thorough here:
- Effectively calls
std::mem_fun_t<Res,T>(f)orstd::const_mem_fun_t<Res,T>(f). - Effectively calls
std::mem_fun1_t<Res,T,Arg>(f)orstd::const_mem_fun1_t<Res,T,Arg>(f).
π¨ okay important lore: this function and the related types were deprecated in C++11 and removed in C++17, in favor of the more generalstd::mem_fnandstd::bindβ both of which create callable adaptor-compatible function objects from member functions. so likeβ¦ she's retired bestie, respect the legacy though π
β¨ Parameters β¨
fβ pointer to a member function to create a wrapper for π
β¨ Return value β¨
A function object wrapping f. that's it, that's the return β¨
β¨ Exceptions β¨
May throw implementation-defined exceptions π (iykyk, it's giving "depends on your compiler's mood")
β¨ Notes β¨
The difference between std::mem_fun and std::mem_fun_ref? Bestie, it's giving subtle but crucial:
std::mem_funproduces a function wrapper that expects a pointer to an object πstd::mem_fun_refβ a reference π
That's the whole distinction fr fr.
β¨ Example β¨
This one demonstrates std::mem_fun usage and compares it with std::mem_fn π
β heads up: a C++11/14 compatible compilation mode might be necessary βg++/clang++with-std=c++11,clwith/std:c++11, etc. And on recent compilers, e.g.gcc-12, might issue "deprecated declaration" warnings if not compiled in C++98 mode π
// Run this code
#include <functional>
#include <iostream>
struct S
{
int get_data() const { return data; }
void no_args() const { std::cout << "void S::no_args() const\n"; }
void one_arg(int) { std::cout << "void S::one_arg()\n"; }
void two_args(int, int) { std::cout << "void S::two_args(int, int)\n"; }
#if __cplusplus > 201100
int data{42};
#else
int data;
S() : data(42) {}
#endif
};
int main()
{
S s;
std::const_mem_fun_t<int, S> p = std::mem_fun(&S::get_data);
std::cout << "s.get_data(): " << p(&s) << '\n';
std::const_mem_fun_t<void, S> p0 = std::mem_fun(&S::no_args);
p0(&s);
std::mem_fun1_t<void, S, int> p1 = std::mem_fun(&S::one_arg);
p1(&s, 1);
#if __cplusplus > 201100
// auto p2 = std::mem_fun(&S::two_args); // Error: mem_fun supports only member functions
// without parameters or with only one parameter.
// Thus, std::mem_fn is a better alternative:
auto p2 = std::mem_fn(&S::two_args);
p2(s, 1, 2);
// auto pd = std::mem_fun(&S::data); // Error: pointers to data members are not supported.
// Use std::mem_fn instead:
auto pd = std::mem_fn(&S::data);
std::cout << "s.data = " << pd(s) << '\n';
#endif
}
β¨ Possible output β¨
s.get_data(): 42
void S::no_args() const
void S::one_arg(int)
void S::two_args(int, int)
s.data = 42
look at that output slaying π β¨
β¨ See also β¨
mem_fn(C++11) β creates a function object out of a pointer to a member (function template) π₯mem_fun_ref(deprecated in C++11) (removed in C++17) β creates a wrapper from a pointer to member function, callable with a reference to object (function template) π¦
and that's the whole page bestie, you ate that π if you ever need her again she lives at http://cppreference.com π β this little archive gem is dated 2024.06.10, so go forth and std::mem_fn responsibly, periodt ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms