c++/c++11 14 17 20

<functional> 99버전 끝

로봇0301 2023. 1. 23. 20:47

std::mem_fun_ref_t

1
2
3
4
5
6
7
8
9
template <class S, class T>
  class mem_fun_ref_t : public unary_function <T,S>
{
  S (T::*pmem)();
public:
  explicit mem_fun_ref_t ( S (T::*p)() ) : pmem (p) {}
  S operator() (T& p) const
    { return (p.*pmem)(); }
};
cs

포인터가 아니라 레퍼런스로 넘겨줘서 호출하고 싶을 때 쓴다

std::mem_fun1_ref_t

1
2
3
4
5
6
7
8
9
template <class S, class T, class A>
  class mem_fun1_ref_t : public binary_function <T,A,S>
{
  S (T::*pmem)(A);
public:
  explicit mem_fun1_ref_t ( S (T::*p)(A) ) : pmem (p) {}
  S operator() (T& p, A x) const
    { return (p.*pmem)(x); }
};
cs

인자 한 개를 고정할 때 쓴다

std::const_mem_fun_ref_t

1
2
3
4
5
6
7
8
9
10
template <class S, class T>
  class const_mem_fun_ref_t : public unary_function <T,S>
{
  S (T::*pmem)() const;
public:
  explicit const_mem_fun_ref_t ( S (T::*p)() const ) : pmem (p) {}
  S operator() (T& p) const
    { return (p.*pmem)(); }
};
 
cs

 

std::const_mem_fun1_ref_t

1
2
3
4
5
6
7
8
9
template <class S, class T, class A>
  class mem_fun1_ref_t : public binary_function <T,A,S>
{
  S (T::*pmem)(A) const;
public:
  explicit mem_fun1_ref_t ( S (T::*p)(A) const ) : pmem (p) {}
  S operator() (T& p, A x) const
    { return (p.*pmem)(x); }
};
cs

const 메서드일 경우 쓴다

std::mem_fun_ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class S, class T>
  mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)())
  { return mem_fun_ref_t<S,T>(f); }
 
template <class S, class T, class A>
  mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A))
  { return mem_fun1_ref_t<S,T,A>(f); }
 
template <class S, class T>
  const_mem_fun_ref_t<S,T> mem_fun_ref (S (T::*f)() const)
  { return const_mem_fun_ref_t<S,T>(f); }
 
template <class S, class T, class A>
  const_mem_fun1_ref_t<S,T,A> mem_fun_ref (S (T::*f)(A) const)
  { return const_mem_fun1_ref_t<S,T,A>(f); }
cs

 

어떻게 쓰는지 찾아봤다.

1
2
3
4
5
6
7
8
9
template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return fn;      // or, since C++11: return move(fn);
}
cs

 

이런 곳에서 쓴다고 한다.

 

다른 예는 못찾겠는데, 아마 템플릿 함수에게만 넘길 수 있게 고안된 것 같다.

 

이 이상 기능의 callback을 만들 일이 있으면 참고해서 만들어다 써야겠다.