std::placeholders:: _1, std::placeholders:: _2, ..., std::placeholders:: _N
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더 파일에 정의됨
<functional>
|
||
|
/*아래 참조*/
_1
;
/*아래 참조*/
_2
;
|
||
std::placeholders
네임스페이스는 플레이스홀더 객체
[_1, ..., _N]
를 포함하며, 여기서
N
은 구현에서 정의된 최대 숫자입니다.
std::bind
표현식에서 인자로 사용될 때, 플레이스홀더 객체는 생성된 함수 객체 내에 저장되며, 해당 함수 객체가 바인딩되지 않은 인자들과 함께 호출될 때 각 플레이스홀더
_N
은 해당하는 N번째 바인딩되지 않은 인자로 대체됩니다.
|
각 자리 표시자는 다음과 같이 선언된 것처럼 선언됩니다 extern /*unspecified*/ _1 ; . |
(C++17까지) |
|
구현체는 자리 표시자를 다음과 같이 선언하는 것이 권장됩니다 inline constexpr /*unspecified*/ _1 ; , 하지만 표준에서는 여전히 extern /*unspecified*/ _1 ; 으로 선언하는 것도 허용됩니다. |
(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
을 언패킹할 때 요소를 건너뛰기 위한 플레이스홀더
(상수) |