On this page
article
std::bind
개념
std::bind
는 callable 객체의 일부 인자를 고정한 새로운 함수 객체를 생성하는 도구- C++ 표준에서 타입을 명확히 지정하지않았으나 operator() 연산자를 재정의한 함수 객체
- 일반 함수, 멤버 함수, 함수 객체, 람다 모두에 사용 가능
- C++11에서 도입
예시
std::ref
- 참조 전달이 필요한 경우
std::ref(n)
사용
인자로 가능한 형태
- 일반 인자
std::ref_wrapper
std::placeholders
(_1
,_2
…)- 다른
bind expression
Bind Expression
std::bind
를 또 다른std::bind
의 인자로 넘기는 방식
개선
→ 2번째 인자가 std::is_bind_expression_v
If T is a type produced by a call to std::bind (but not std::bind_front or std:: bind_back), this template is derived from std::true_type. For any other type (unless user-specialized), this template is derived from std::false_type.
bind_front (C++20), bind_back (C++23)
std::bind
의 복잡성과_2
,_1
인덱싱의 혼란을 개선
요약
std::bind
는 인자를 고정해 새로운 callable 객체를 만들 수 있음- 중첩된 표현도 가능하지만 복잡해질 수 있음
- C++20 이후
std::bind_front
, C++23 이후std::bind_back
사용 권장