GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::mem_fun(3) C++ Standard Libary std::mem_fun(3)

std::mem_fun - std::mem_fun


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 );


Creates a member function wrapper object, deducing the target type from the template
arguments. The wrapper object expects a pointer to an object of type T as the first
parameter to its operator().


1) Effectively calls std::mem_fun_t<Res,T>(f) or std::const_mem_fun_t<Res,T>(f).
2) Effectively calls std::mem_fun1_t<Res,T,Arg>(f) or
std::const_mem_fun1_t<Res,T,Arg>(f).


This function and the related types were deprecated in C++11 and removed in C++17 in
favor of the more general std::mem_fn and std::bind, both of which create callable
adaptor-compatible function objects from member functions.


f - pointer to a member function to create a wrapper for


A function object wrapping f.


May throw implementation-defined exceptions.


The difference between std::mem_fun and std::mem_fun_ref is that the former produces
an function wrapper that expects a pointer to an object, whereas the latter -- a
reference.


demonstrates std::mem_fun usage and compares it with std::mem_fn. C++11/14
compatible compilation mode might be necessary (e.g. g++/clang++ with -std=c++11, cl
with /std:c++11).

// Run this code


#include <iostream>
#include <functional>


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"; }
int data{42};
};


int main() {
S s;


auto p = std::mem_fun(&S::get_data);
std::cout << "s.get_data(): " << p(&s) << '\n';


auto p0 = std::mem_fun(&S::no_args);
p0(&s);
auto p1 = std::mem_fun(&S::one_arg);
p1(&s, 1);


// 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';
}


s.get_data(): 42
void S::no_args() const
void S::one_arg(int)
void S::two_args(int, int)
s.data = 42


mem_fn creates a function object out of a pointer to a member
(C++11) (function template)
mem_fun_ref creates a wrapper from a pointer to member function, callable
(deprecated in C++11) with a reference to object
(removed in C++17) (function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.