|
需要注意的是,本節題目中"函數對象"四字是加了雙引號的,因為C語言中沒有辦法做到類C++中函數對象的做法,CGL中采用的替代手段是使用函數指針. 首先需要短暫回顧STL中的函數對象以及函數對象適配器等概念. STL中的最基本的函數對象分為三類: generator, unary function,binary function,其中的generator是無參數函數,unary function是單參數函數,binary function是雙參數函數.注意,以上只是提到的三者的傳入參數的數量,還沒有涉及到它們所要返回的參數類型.一般的,三者都是以template參數作為自己返回參數的類型,但是當unary function和binary function返回參數是bool型的時候,分別稱它們為:predicate和binary predicate,這是因為STL中很多范型參數的調用需要傳入一個函數對象進行元素的比較.如sort,find_if等函數.
函數對象適配器分為兩類:一類是綁定器(binder),另外一類是取反器(negator),綁定器用于綁定函數對象中的某個參數,取反器用于將函數對象的返回值進行翻轉,一般的,可以使用取反器的函數對象都是那些返回值為bool型的函數對象.
CGL中不能實現函數適配器,因為綁定器在綁定參數的時候是通過將所需要綁定的參數作為模版參數傳遞給函數對象類進行參數綁定的,如:
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);
}
};

template <class Operation, class T>
inline binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
typedef typename Operation::second_argument_type arg2_type;
return binder2nd<Operation>(op, arg2_type(x));
}


以上是綁定器bind2nd函數的實現,你可以看到這個函數是返回一個binder2nd的函數對象類,而所需要綁定的參數作為binder2nd的value參數存儲起來,在調用binder2nd的operator()時再使用value參數,由于C中沒有辦法做到重載operator()函數,所以不能實現binder.
同樣的,C中也沒有辦法實現negator函數對象適配器,見代碼:
template <class Predicate>
class unary_negate
: public unary_function<typename Predicate::argument_type, bool> {
protected:
Predicate pred;
public:
explicit unary_negate(const Predicate& x) : pred(x) {}
bool operator()(const typename Predicate::argument_type& x) const {
return !pred(x);
}
};

template <class Predicate>
inline unary_negate<Predicate> not1(const Predicate& pred) {
return unary_negate<Predicate>(pred);
}


以上是negator函數not1函數的實現.可以看到這個函數返回一個unary_negate函數對象,而在這個函數對象重載的operator()中對pred函數的返回值進行取反以達到negator函數對象適配器的作用,同樣的,用于C中不能做到重載operator(),所以沒有辦法實現negator函數對象適配器.
STL中還有一些預定義的函數對象如plus,equal_to等,見代碼:
template <class T>
struct plus : public binary_function<T, T, T> {
T operator()(const T& x, const T& y) const { return x + y; }
};


plus的函數參數是通過模版參數T傳入的,任何想要采用plus的類型都需要提供operator + 操作符.因為這個特點,C中也沒有辦法實現這些預定義的函數對象.
鑒于以上的原因,CGL中的"函數對象"被定義為幾種函數指針:
typedef bool_t (*binary_predicate)(data_t tData1, data_t tData2);
typedef void (*binary_func)(data_t tData1, data_t tData2, data_t tResult);
typedef void (*binary_func2)(data_t tData1, data_t tData2);

typedef bool_t (*unary_predicate)(data_t tData);
typedef void (*unary_func)(data_t tData, data_t tResult);
typedef void (*unary_func2)(data_t tData);

typedef void (*generator_func)(data_t tResult);
typedef void (*generator_func2)();


這些函數指針的返回值只有兩種,bool_t和void,當需要返回除了boot_t之外的返回值時,將保存返回值的指針tResult傳入函數中保存返回值,所有CGL中使用的函數指針都只可能是以上的幾種函數指針類型.
|