_1 オブジェクト
更新 : 2007 年 11 月
置き換え可能な引数のプレースホルダです。
namespace placeholders {
extern unspecified _1, _2, ... _M
} // namespace placeholders (within std::tr1)
解説
_1, _2, ... _M の各オブジェクトは、bind 関数 によって返されたオブジェクトに対する関数呼び出しにおいて、それぞれ 1 番目から M 番目の引数を指定するプレースホルダです。bind 式が評価されるときに N 番目の引数が挿入される場所を指定するには、_N を使用します。
この実装では、M の値は 10 です。
使用例
// std_tr1__functional__placeholder.cpp
// compile with: /EHsc
#include <functional>
#include <algorithm>
#include <iostream>
using namespace std::tr1::placeholders;
void square(double x)
{
std::cout << x << "^2 == " << x * x << std::endl;
}
void product(double x, double y)
{
std::cout << x << "*" << y << " == " << x * y << std::endl;
}
int main()
{
double arg[] = {1, 2, 3};
std::for_each(&arg[0], &arg[3], square);
std::cout << std::endl;
std::for_each(&arg[0], &arg[3], std::tr1::bind(product, _1, 2));
std::cout << std::endl;
std::for_each(&arg[0], &arg[3], std::tr1::bind(square, _1));
return (0);
}
1^2 == 1
2^2 == 4
3^2 == 9
1*2 == 2
2*2 == 4
3*2 == 6
1^2 == 1
2^2 == 4
3^2 == 9
必要条件
ヘッダー : <functional>
名前空間 : std::tr1