std::is_bind_expression
来自 zh.cppreference.net
| 定义于头文件 <functional>
|
||
template< class T >
struct is_bind_expression;
|
(自 C++11 起) | |
如果 T 是由调用 std::bind(而非 std::bind_front 或 std::bind_front)产生的类型,则此模板派生自 std::true_type。对于任何其他类型(除非用户特化),此模板派生自 std::false_type。
程序可以为程序定义类型 T特化此模板,以基特征为 std::true_type 实现 UnaryTypeTrait,从而表明 T 应被 std::bind 当作绑定子表达式的类型对待:当调用 bind 生成的函数对象时,此类型的绑定实参将作为函数对象被调用,并接收所有传递给 bind 生成对象的未绑定实参。
辅助变量模板
|
template
<
class
T
>
constexpr bool is_bind_expression_v = is_bind_expression < T > :: value ; |
(C++17 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
是由
std::bind
生成的函数对象则为
true
,否则为
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() { // 效果等同于 bind(f, bind(MyBind(), _1, _2), 2) auto b = std::bind(f, MyBind(), 2); std::cout << "将2与10和11的和相加得到 " << b(10, 11) << '\n'; }
输出:
将2与10和11的和相加得到 23
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2010 | C++11 |
程序定义的特化只能
继承自 std::false_type |
可以继承自
std::true_type |
参见
|
(C++11)
|
将一个或多个参数绑定到函数对象
(函数模板) |