std::binder1st, std::binder2nd
cppreference.net에서
| 헤더 <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) | (C++11에서 폐지) (C++17에서 제거) |
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) | (C++11에서 폐지) (C++17에서 제거) |
이진 함수에 인수를 바인딩하는 함수 객체입니다.
매개변수의 값은 객체 생성 시 전달되어 객체 내부에 저장됩니다. 함수 객체가 operator()을 통해 호출될 때마다 저장된 값이 인수 중 하나로 전달되고, 다른 인수는 operator()의 인수로 전달됩니다. 결과 함수 객체는 단항 함수입니다.
1) 객체 생성 시 주어진 값
value에 첫 번째 매개변수를 바인딩합니다.2) 객체 생성 시 주어진 값
value에 두 번째 매개변수를 바인딩합니다.예제
이 코드 실행
#include <cmath> #include <functional> #include <iostream> #include <vector> const double pi = std::acos(-1); // C++20에서는 std::numbers::pi 사용 int main() { // C++11에서 deprecated, C++17에서 제거됨 auto f1 = std::bind1st(std::multiplies<double>(), pi / 180.0); // C++11 대체 방법 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 (바인더 사용)\t= " << f2(n) << " rad (람다 사용)\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()
에 전달된 인수를 수정할 수 없었음
|
이를 처리하기 위한 오버로드 추가됨 |
참고 항목
|
(C++11에서 사용 중단됨)
(C++17에서 제거됨)
|
이항 함수에 하나의 인수를 바인딩함
(함수 템플릿) |