std::is_placeholder
From cppreference.com
< cpp | utility | functional
| ヘッダ <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> を基底特性とし、T を N 番目のプレースホルダ型として扱うことを示す正の N を実装できます。
std::bind は、バインドされていない引数のプレースホルダを検出するために std::is_placeholder を使用します。
目次 |
[編集] ヘルパー変数テンプレート
| template< class T > constexpr int is_placeholder_v = is_placeholder<T>::value; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
| value [static] |
プレースホルダの値、またはプレースホルダでない型の場合は 0 (公開静的メンバ定数) |
メンバ関数
| operator int |
オブジェクトを int に変換し、value を返します。 (public member function) |
| operator() (C++14) |
value を返します。 (public member function) |
メンバ型
| 型 | 定義 |
value_type
|
int |
type
|
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) |
1つ以上の引数を関数オブジェクトに束縛する (関数テンプレート) |
| (C++11) |
std::bind 式における未束縛の引数のためのプレースホルダ(定数) |