C++中函数对象之函数适配器的bind2nd问题
在学函数适配器时,我参考了cplusplus.com的reference,感觉里面有一处不是很明白:
为什么 binder2nd的value成员不声明为引用,不用引用,肯定会有复制构造,如果用引用这次复制是可以避免的啊。而且binder2nd的构造函数使得second_argument_type本身不能是引用。
本人菜鸟,欢迎各位回答:
下面是cplusplus.com的标准库,bind2nd和binder2nd代码:
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}
template <class Operation> class binder2nd
: public unary_function <typename Operation::first_argument_type,
typename Operation::result_type>
{
protected:
Operation op;
typename Operation::second_argument_type value;
public:
binder2nd ( const Operation& x,
const typename Operation::second_argument_type& y) : op (x), value(y) {}
typename Operation::result_type
operator() (const typename Operation::first_argument_type& x) const
{ return op(x,value); }
};