std::is_bind_expression
来自 cppreference.cn
< cpp | utility | functional
定义于头文件 <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 视为绑定子表达式的类型:当调用 bind 生成的函数对象时,此类型的绑定参数将作为函数对象被调用,并将获得传递给 bind 生成对象的所有未绑定参数。
目录 |
[编辑] 辅助变量模板
template< class T > constexpr bool is_bind_expression_v = is_bind_expression<T>::value; |
(自 C++17 起) | |
继承自 std::integral_constant
成员常量
值 [静态] |
true 如果 T 是 std::bind 生成的函数对象,false 否则为 false(公共静态成员常量) |
成员函数
operator bool |
将对象转换为 bool,返回 value (公共成员函数) |
operator() (C++14) |
返回 value (公共成员函数) |
成员类型
类型 | 定义 |
value_type
|
bool |
type
|
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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2010 | C++11 | 程序定义的特化只能派生自 std::false_type 只能派生自 std::false_type |
可以派生自 std::true_type |
[编辑] 参见
(C++11) |
将一个或多个参数绑定到函数对象 (函数模板) |