std::is_placeholder
来自cppreference.com
< cpp | utility | functional
在标头 <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
特化此模板,以拥有 N 是正数的 std::integral_constant<int, N> 的基特征实现一元类型特征(UnaryTypeTrait) ,指示 T
应被处理成第 N 个占位符类型。
std::bind 用 std::is_placeholder
检测代表未绑定实参的占位符。
目录 |
[编辑]辅助变量模板
template<class T > constexprint is_placeholder_v = is_placeholder<T>::value; | (C++17 起) | |
继承自 std::integral_constant
成员常量
value [静态] | 占位符值,或对于非占位符类型为 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<decltype(std::placeholders::_5)>::value<<" 个实参\n"; auto b =std::bind(f, my_2, 2);std::cout<<"以自定义占位符选择的 11 加上 2 得到 "<< b(10, 11)// 忽略首个实参,即 10<<'\n';}
输出:
标准占位符 _5 对应第 5 个实参 以自定义占位符选择的 11 加上 2 得到 13
[编辑]参阅
(C++11) | 绑定一或多个实参到函数对象 (函数模板) |
(C++11) | 用作 std::bind 表达式中的未绑定实参的占位符 (常量) |