您的位置:首页 > 其它

测试functional的bind以及相关功能

2014-05-06 17:55 253 查看
注:在VS2010 UPDATE1下测试通过

/*测试functional的bind以及相关功能*/

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;

int TestAdd(int a, int b)
{
return a+b;
}
bool TestCompare(int a, int b)
{
return a<b;
}
class TestBindClass
{
public:
bool operator() (const int &a, const int &b) const
{
cout << a << "<" << b << " ? " << ( a<b ? "true" : "false" ) << endl;
return a<b;
}
typedef int first_argument_type;
typedef int second_argument_type;
typedef bool result_type;
};
template <class T> struct greater1
{
bool operator() (const T& x, const T& y) const
{
cout << x << ">" << y << " ? " << ( x>y ? "true" : "false" ) << endl;
return x>y;
}
//void operator() (const T& a, const T& b) const {cout << "a<b ? " << ( a<b ? "true" : "false" ) << endl;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};

void main()
{
//test bind function
auto fcn1 = bind(TestAdd, 5, 10);
cout << fcn1() << endl;

//test bind function
auto fcn2 = bind(TestCompare, _1, 5);
cout << fcn2(4) << endl;

//test binder1st function
binder1st<greater1<int>> fcn4(greater1<int>(), 6);
cout << fcn4(7) << endl;

//test binder1st function
binder1st<TestBindClass> fcn3(TestBindClass(), 5);
cout << fcn3(3) << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: