functional_is_placeholder

functional_is_placeholder (자리 표시자 판별)

이 페이지에서는 C++ 표준 라이브러리의 std::is_placeholder 템플릿에 대해 설명해요. 이 템플릿은 주어진 타입이 표준 자리 표시자(_1, _2, _3, ...)인지 판별하고, 해당하는 자리 표시자 번호를 값으로 제공해요. std::bind가 바인딩되지 않은 인자를 처리할 때 이 특성을 활용해요.

출처: cppreference

본문

정의

<functional> 헤더에 정의되어 있어요.

<functional> 헤더에 정의됨
template < class T > struct is_placeholder; (since C++11)

T가 표준 자리 표시자(_1, _2, _3, ...)의 타입이라면, 이 템플릿은 각각 std::integral_constant<int, 1>, std::integral_constant<int, 2>, std::integral_constant<int, 3>에서 파생돼요.

T가 표준 자리 표시자 타입이 아니라면, 이 템플릿은 std::integral_constant<int, 0>에서 파생돼요.

프로그램은 프로그램 정의 타입 T에 대해 이 템플릿을 특수화할 수 있어요. 이때 기본 특성으로 std::integral_constant<int, N>(양의 N)을 사용하는 UnaryTypeTrait을 구현하면, T가 N번째 자리 표시자 타입으로 취급되어야 함을 나타낼 수 있어요.

std::bind는 바인딩되지 않은 인자를 위한 자리 표시자를 감지하기 위해 std::is_placeholder를 사용해요.

헬퍼 변수 템플릿

template < class T > constexpr int is_placeholder_v = is_placeholder<T>::value; (since C++17)

std::integral_constant에서 상속

멤버 상수

value [static] 자리 표시자 값, 또는 자리 표시자가 아닌 타입에 대해서는 0 (공용 정적 멤버 상수)

멤버 함수

operator int 객체를 int로 변환하고 value를 반환해요 (공용 멤버 함수)
operator() (C++14) value를 반환해요 (공용 멤버 함수)

멤버 타입

타입 정의
value_type int
type std::integral_constant<int, value>

예제

#include <functional>
#include <iostream>
#include <type_traits>

struct My_2 {} my_2;

namespace std
{
    template<>
    struct is_placeholder<My_2> : public integral_constant<int, 2> {};
}

int f(int n1, int n2)
{
    return n1 + n2;
}

int main()
{
    std::cout << "Standard placeholder _5 is for the argument number "
              << std::is_placeholder_v<decltype(std::placeholders::_5)>
              << '\n';
    
    auto b = std::bind(f, my_2, 2);
    std::cout << "Adding 2 to 11 selected with a custom placeholder gives " 
              << b(10, 11) // the first argument, namely 10, is ignored
              << '\n';
}

출력:

Standard placeholder _5 is for the argument number 5
Adding 2 to 11 selected with a custom placeholder gives 13

같이 보기

bind (C++11) 함수 객체에 하나 이상의 인자를 바인딩해요 (함수 템플릿) [edit]
_1, _2, _3, _4, ... (C++11) std::bind 표현식에서 바인딩되지 않은 인자를 위한 자리 표시자 (상수) [edit]

더 알아보기 (Learn more)

cppreference