Namespaces
Variants

std:: mem_fun

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
mem_fun
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( 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++20* )
( until 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에서 사용 중단(deprecated)되었으며 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에서 제거됨)
객체에 대한 참조로 호출 가능한 멤버 함수 포인터로부터 래퍼를 생성함
(함수 템플릿)