std::placeholders:: _1, std::placeholders:: _2, ..., std::placeholders:: _N
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
ヘッダーで定義
<functional>
|
||
|
/* 実装定義 */
_1
;
/* 実装定義 */
_2
;
|
||
std::placeholders
名前空間はプレースホルダーオブジェクト
[_1, ..., _N]
を含みます。ここで
N
は実装定義の最大数です。
引数として
std::bind
式で使用される場合、プレースホルダーオブジェクトは生成された関数オブジェクト内に格納され、その関数オブジェクトが未束縛引数で呼び出されると、各プレースホルダー
_N
は対応するN番目の未束縛引数で置き換えられます。
|
各プレースホルダーは、以下のように宣言されているかのように扱われる: extern /*unspecified*/ _1 ; 。 |
(C++17まで) |
|
実装は、プレースホルダーを以下のように宣言することが推奨される: inline constexpr /*unspecified*/ _1 ; 。ただし、標準では以下のように宣言することも引き続き許可されている: extern /*unspecified*/ _1 ; 。 |
(C++17以降) |
プレースホルダーオブジェクトの型は
DefaultConstructible
および
CopyConstructible
であり、それらのデフォルトのコピー/ムーブコンストラクタは例外を送出せず、任意のプレースホルダー
_N
に対して、型
std::
is_placeholder
<
decltype
(
_N
)
>
が定義され、ここで
std::
is_placeholder
<
decltype
(
_N
)
>
は
std::
integral_constant
<
int
, N
>
から派生する。
例
以下のコードは、プレースホルダー引数を使用した関数オブジェクトの作成を示しています。
#include <functional> #include <iostream> #include <string> void goodbye(const std::string& s) { std::cout << "Goodbye " << s << '\n'; } class Object { public: void hello(const std::string& s) { std::cout << "Hello " << s << '\n'; } }; int main() { using namespace std::placeholders; using ExampleFunction = std::function<void(const std::string&)>; Object instance; std::string str("World"); ExampleFunction f = std::bind(&Object::hello, &instance, _1); f(str); // equivalent to instance.hello(str) f = std::bind(&goodbye, std::placeholders::_1); f(str); // equivalent to goodbye(str) auto lambda = [](std::string pre, char o, int rep, std::string post) { std::cout << pre; while (rep-- > 0) std::cout << o; std::cout << post << '\n'; }; // binding the lambda: std::function<void(std::string, char, int, std::string)> g = std::bind(&decltype(lambda)::operator(), &lambda, _1, _2, _3, _4); g("G", 'o', 'o'-'g', "gol"); }
出力:
Hello World Goodbye World Goooooooogol
関連項目
|
(C++11)
|
1つ以上の引数を関数オブジェクトにバインドする
(関数テンプレート) |
|
(C++11)
|
オブジェクトが標準プレースホルダーであるか、プレースホルダーとして使用できることを示す
(クラステンプレート) |
|
(C++11)
|
tie
を使用して
tuple
を展開する際に要素をスキップするためのプレースホルダー
(定数) |