Namespaces
Variants

std::is_placeholder

来自 zh.cppreference.net
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧的绑定器与适配器
(至 C++17*)
(至 C++17*)
(至 C++17*)
(至 C++17*)
(至 C++17*)(至 C++17*)(至 C++17*)(至 C++17*)
(至 C++20*)
(至 C++20*)
(至 C++17*)(至 C++17*)
(至 C++17*)(至 C++17*)

(至 C++17*)
(至 C++17*)(至 C++17*)(至 C++17*)(至 C++17*)
(至 C++20*)
(至 C++20*)
 
定义于头文件 <functional>
template< class T >
struct is_placeholder;
(自 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特化此模板以实现 UnaryTypeTrait,其基特征为 std::integral_constant<int, N> 并带有正数 N,以指示 T 应被当作第 N 个 占位符类型。

std::bind 使用 std::is_placeholder 来检测未绑定实参的占位符。

辅助变量模板

template < class T >
constexpr int is_placeholder_v = is_placeholder < T > :: value ;
(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 << "标准占位符 _5 对应的参数序号为 "
              << std::is_placeholder_v<decltype(std::placeholders::_5)>
              << '\n';
    auto b = std::bind(f, my_2, 2);
    std::cout << "使用自定义占位符将 2 与 11 相加得到 " 
              << b(10, 11) // 第一个参数(即 10)被忽略
              << '\n';
}

输出:

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

参见

(C++11)
将一个或多个参数绑定到函数对象
(函数模板)
std::bind 表达式中未绑定参数的占位符
(常量)