1. 程式人生 > >自定義c++函式介面卡練習

自定義c++函式介面卡練習

/**
* 自定義函式介面卡,該函式介面卡會將二元函式物件變成一元函式物件
*/
template<typename BinaryFunction>
class MyBinder2nd : public unary_function<typename BinaryFunction::first_argument_type, typename BinaryFunction::result_type>{
public:
	MyBinder2nd(BinaryFunction& bf, const typename BinaryFunction::second_argument_type& arg_2){
		this->bf = bf;
		this->arg_2 = arg_2;
	}

	typename BinaryFunction::result_type operator()(typename BinaryFunction::first_argument_type& arg_1){
		return bf(arg_1, arg_2);
	}

protected:
	BinaryFunction bf;
	typename BinaryFunction::second_argument_type arg_2;
};

void test10(){
	plus<int> pl;
	/*int result1 = pl(3, 4);
	cout << result1 << endl;*/
	MyBinder2nd<plus<int>> my_bind(pl, 10);
	int x = 129;
	int result2 = my_bind(x);
	cout << result2 << endl;
}