std::is_bind_expression
来自 cppreference.cn
定义于头文件 <functional> |
||
template< class T > struct is_bind_expression; |
(C++11 起) | |
如果 T
是由 std::bind (而非 std::bind_front 或 std::bind_back)调用产生的类型,则此模板派生自 std::true_type。对于任何其他类型(除非用户特化),此模板派生自 std::false_type。
程序可以为 程序定义类型 T
特化此模板以实现 UnaryTypeTrait,其基本特性为 std::true_type,以指示 T
应该被 std::bind 视为绑定子表达式的类型:当调用绑定生成的函数对象时,此类型的绑定参数将被作为函数对象调用,并将获得传递给绑定生成对象的所有未绑定参数。
目录 |
[编辑] 辅助变量模板
template< class T > constexpr bool is_bind_expression_v = is_bind_expression<T>::value; |
(C++17 起) | |
继承自 std::integral_constant
成员常量
value [静态] |
如果 T 是由 std::bind 生成的函数对象,则为 true,否则为 false(public static 成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公开成员函数) |
operator() (C++14) |
返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
类型
|
std::integral_constant<bool, value> |
[编辑] 示例
运行此代码
#include <functional> #include <iostream> #include <type_traits> struct MyBind { typedef int result_type; int operator()(int a, int b) const { return a + b; } }; namespace std { template<> struct is_bind_expression<MyBind> : public true_type {}; } int f(int n1, int n2) { return n1 + n2; } int main() { // as if bind(f, bind(MyBind(), _1, _2), 2) auto b = std::bind(f, MyBind(), 2); std::cout << "Adding 2 to the sum of 10 and 11 gives " << b(10, 11) << '\n'; }
输出
Adding 2 to the sum of 10 and 11 gives 23
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2010 | C++11 | 程序定义的特化可能 只能派生自 std::false_type |
可以派生自 std::true_type |
[编辑] 参阅
(C++11) |
将一个或多个参数绑定到函数对象 (函数模板) |