functional_binder12
functional_binder12 (binder1st/binder2nd 바인더 함수 객체)
이 페이지는 C++ 표준 라이브러리의 binder1st와 binder2nd 클래스 템플릿에 대해 설명해요. 이들은 이진 함수의 한 인자를 생성 시점에 고정하여 단항 함수 객체로 만들어 주는 도구예요. C++11에서 더 이상 사용되지 않게 되었고 C++17에서 제거되었어요.
출처: cppreference
본문
정의
<functional> 헤더에 정의되어 있어요.
| 정의 | 버전 |
|---|---|
template < class Fn > class binder1st : public std::unary_function < typename Fn::second_argument_type , typename Fn::result_type > { protected: Fn op; typename Fn::first_argument_type value; public: binder1st(const Fn& fn, const typename Fn::first_argument_type& value); typename Fn::result_type operator()(const typename Fn::second_argument_type& x) const; typename Fn::result_type operator()(typename Fn::second_argument_type& x) const; }; |
(1) |
template < class Fn > class binder2nd : public std::unary_function < typename Fn::first_argument_type , typename Fn::result_type > { protected: Fn op; typename Fn::second_argument_type value; public: binder2nd(const Fn& fn, const typename Fn::second_argument_type& value); typename Fn::result_type operator()(const typename Fn::first_argument_type& x) const; typename Fn::result_type operator()(typename Fn::first_argument_type& x) const; }; |
(2) |
설명
이진 함수에 인자를 하나 고정하는 함수 객체예요.
인자의 값은 객체를 생성할 때 전달되어 객체 내부에 저장돼요. 함수 객체가 operator()를 통해 호출될 때마다 저장된 값이 인자 중 하나로 전달되고, 나머지 인자는 operator()의 인자로 전달돼요. 결과적으로 만들어지는 함수 객체는 단항 함수가 돼요.
예제
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
const double pi = std::acos(-1); // use std::numbers::pi in C++20
int main()
{
// deprecated in C++11, removed in C++17
auto f1 = std::bind1st(std::multiplies<double>(), pi / 180.0);
// C++11 replacement
auto f2 = [](double a) { return a * pi / 180.0; };
for (double n : {0, 30, 45, 60, 90, 180})
std::cout << n << "°\t" << std::fixed << "= "
<< f1(n) << " rad (using binder)\t= "
<< f2(n) << " rad (using lambda)\n"
<< std::defaultfloat;
}
출력:
0° = 0.000000 rad (using binder) = 0.000000 rad (using lambda)
30° = 0.523599 rad (using binder) = 0.523599 rad (using lambda)
45° = 0.785398 rad (using binder) = 0.785398 rad (using lambda)
60° = 1.047198 rad (using binder) = 1.047198 rad (using lambda)
90° = 1.570796 rad (using binder) = 1.570796 rad (using lambda)
180° = 3.141593 rad (using binder) = 3.141593 rad (using lambda)
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | 적용 대상 | 발표된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 109 | C++98 | operator()가 전달된 인자를 수정할 수 없었음 |
이를 처리하는 오버로드를 추가함 |
같이 보기
bind1st bind2nd (C++11에서 폐기, C++17에서 제거) |
이진 함수의 한 인자를 고정함 (함수 템플릿) |
|---|