std::is_placeholder
來自 cppreference.com
定義於標頭檔案 <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
,以實現 UnaryTypeTrait,其基本特性為 std::integral_constant<int, N>,其中正整數 N 表示 T
應被視為第 N 個佔位符型別。
std::bind 使用 std::is_placeholder
來檢測未繫結引數的佔位符。
目錄 |
[編輯] 輔助變數模板
template< class T > constexpr int is_placeholder_v = is_placeholder<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
佔位符值或非佔位符型別的 0 (public static 成員常量) |
成員函式
operator int |
將物件轉換為 int,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
int |
型別
|
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 << "Standard placeholder _5 is for the argument number " << std::is_placeholder_v<decltype(std::placeholders::_5)> << '\n'; auto b = std::bind(f, my_2, 2); std::cout << "Adding 2 to 11 selected with a custom placeholder gives " << b(10, 11) // the first argument, namely 10, is ignored << '\n'; }
輸出
Standard placeholder _5 is for the argument number 5 Adding 2 to 11 selected with a custom placeholder gives 13
[編輯] 參閱
(C++11) |
將一個或多個引數繫結到函式物件 (函式模板) |
(C++11) |
std::bind 表示式中未繫結引數的佔位符(常數) |