std::placeholders::_1, std::placeholders::_2, ..., std::placeholders::_N
cppreference.net에서 가져옴
| 헤더 <functional>
|
||
/* see below */ _1;
/* see below */ _2;
.
.
/* see below */ _N;
|
||
에서 정의됨std::placeholders 네임스페이스에는 플레이스홀더 객체 [_1, ..., _N]가 포함되어 있으며, 여기서 N는 구현에서 정의된 최대 개수입니다.
이들이 std::bind 표현식의 인수로 사용될 때, 플레이스홀더 객체는 생성된 함수 객체에 저장되며, 해당 함수 객체가 바인드되지 않은 인수로 호출되면 각 플레이스홀더 _N는 해당하는 N번째 바인드되지 않은 인수로 대체됩니다.
|
각 플레이스홀더는 |
(C++17까지) |
|
구현은 플레이스홀더를 |
(C++17부터) |
플레이스홀더 객체의 타입은 DefaultConstructible 및 CopyConstructible이며, 기본 복사/이동 생성자는 예외를 던지지 않으며, 임의의 플레이스홀더 _N에 대해 타입 std::is_placeholder<decltype(_N)>이 정의되며, 여기서 std::is_placeholder<decltype(_N)>는 std::integral_constant<int, N>에서 파생됩니다.
예제
다음 코드는 플레이스홀더 인수를 사용한 함수 객체 생성 방법을 보여줍니다.
이 코드 실행
#include <functional> #include <iostream> #include <string> void goodbye(const std::string& s) { std::cout << "Goodbye " << s << '\n'; } class Object { public: void hello(const std::string& s) { std::cout << "Hello " << s << '\n'; } }; int main() { using namespace std::placeholders; using ExampleFunction = std::function<void(const std::string&)>; Object instance; std::string str("World"); ExampleFunction f = std::bind(&Object::hello, &instance, _1); f(str); // equivalent to instance.hello(str) f = std::bind(&goodbye, std::placeholders::_1); f(str); // equivalent to goodbye(str) auto lambda = [](std::string pre, char o, int rep, std::string post) { std::cout << pre; while (rep-- > 0) std::cout << o; std::cout << post << '\n'; }; // binding the lambda: std::function<void(std::string, char, int, std::string)> g = std::bind(&decltype(lambda)::operator(), &lambda, _1, _2, _3, _4); g("G", 'o', 'o'-'g', "gol"); }
출력:
Hello World Goodbye World Goooooooogol
참고 항목
|
(C++11)
|
하나 이상의 인수를 함수 객체에 바인딩합니다
(함수 템플릿) |
|
(C++11)
|
객체가 표준 플레이스홀더이거나 플레이스홀더로 사용될 수 있음을 나타냅니다
(클래스 템플릿) |
|
(C++11)
|
tie
를 사용하여
tuple
을 언패킹할 때 요소를 건너뛰기 위한 플레이스홀더
(상수) |