std::is_bind_expression
來自 cppreference.com
定義於標頭檔案 <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) |
將一個或多個引數繫結到函式物件 (函式模板) |