Namespaces
Variants

std::mem_fun

cppreference.com에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
교환 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
함수 객체
함수 호출
(C++17)(C++23)
항등 함수 객체
(C++20)
투명 연산자 래퍼
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

기존 바인더 및 어댑터
(C++17까지*)
(C++17까지*)
(C++17까지*)
(C++17까지*)  
(C++17까지*)
(C++17까지*)(C++17까지*)(C++17까지*)(C++17까지*)
(C++20까지*)
(C++20까지*)
(C++17까지*)(C++17까지*)
(C++17까지*)(C++17까지*)

(C++17까지*)
(C++17까지*)(C++17까지*)(C++17까지*)(C++17까지*)
(C++20까지*)
(C++20까지*)
 
헤더 <functional>
template< class Res, class T >
std::mem_fun_t<Res,T> mem_fun( Res (T::*f)() );
에 정의됨 (1) (C++11에서 폐기됨)
(C++17에서 제거됨)
template< class Res, class T >
std::const_mem_fun_t<Res,T> mem_fun( Res (T::*f)() const );
(1) (C++11에서 폐기됨)
(C++17에서 제거됨)
template< class Res, class T, class Arg >
std::mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) );
(2) (C++11에서 폐기됨)
(C++17에서 제거됨)
template< class Res, class T, class Arg >
std::const_mem_fun1_t<Res,T,Arg> mem_fun( Res (T::*f)(Arg) const );
(2) (C++11에서 폐기됨)
(C++17에서 제거됨)

템플릿 인자에서 대상 타입을 추론하여 멤버 함수 래퍼 객체를 생성합니다. 래퍼 객체는 T 타입의 객체에 대한 포인터를 자신의 operator() 첫 번째 매개변수로 기대합니다.

1) 효과적으로 std::mem_fun_t<Res,T>(f) 또는 std::const_mem_fun_t<Res,T>(f)를 호출합니다.
2) 효과적으로 std::mem_fun1_t<Res,T,Arg>(f) 또는 std::const_mem_fun1_t<Res,T,Arg>(f)를 호출합니다.

이 함수와 관련 타입은 C++11에서 폐기되고 C++17에서 제거되었으며, 대신 더 일반적인 std::mem_fn과 std::bind를 사용하는 것이 권장됩니다. 이 둘 모두 멤버 함수에서 호출 가능한 어댑터 호환 함수 객체를 생성합니다.

매개변수

f - 래퍼를 생성할 멤버 함수에 대한 포인터

반환값

함수 객체가 f 를 래핑합니다.

예외

구현 정의 예외를 던질 수 있습니다.

참고 사항

std :: mem_fun 와 std:: mem_fun_ref 의 차이점은 전자는 객체에 대한 포인터를 기대하는 함수 래퍼를 생성하는 반면, 후자는 참조를 기대한다는 점입니다.

예제

std::mem_fun 사용법을 보여주고 이를 std::mem_fn 과 비교합니다. C++11/14 호환 컴파일 모드가 필요할 수 있습니다: g++/clang++에서 -std=c++11, cl에서 /std:c++11 등. 최신 컴파일러(예: gcc-12)에서는 C++98 모드로 컴파일하지 않으면 "deprecated declaration" 경고가 발생할 수 있습니다.

#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
}

가능한 출력:

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

참고 항목

(C++11)
멤버 포인터로부터 함수 객체를 생성함
(함수 템플릿)
(C++11에서 사용 중단됨) (C++17에서 제거됨)
객체에 대한 참조로 호출 가능한 멤버 함수 포인터로부터 래퍼를 생성함
(함수 템플릿)