|
|
需要注意的是,本節(jié)題目中"函數(shù)對象"四字是加了雙引號的,因為C語言中沒有辦法做到類C++中函數(shù)對象的做法,CGL中采用的替代手段是使用函數(shù)指針. 首先需要短暫回顧STL中的函數(shù)對象以及函數(shù)對象適配器等概念. STL中的最基本的函數(shù)對象分為三類: generator, unary function,binary function,其中的generator是無參數(shù)函數(shù),unary function是單參數(shù)函數(shù),binary function是雙參數(shù)函數(shù).注意,以上只是提到的三者的傳入?yún)?shù)的數(shù)量,還沒有涉及到它們所要返回的參數(shù)類型.一般的,三者都是以template參數(shù)作為自己返回參數(shù)的類型,但是當unary function和binary function返回參數(shù)是bool型的時候,分別稱它們?yōu)?predicate和binary predicate,這是因為STL中很多范型參數(shù)的調(diào)用需要傳入一個函數(shù)對象進行元素的比較.如sort,find_if等函數(shù).
函數(shù)對象適配器分為兩類:一類是綁定器(binder),另外一類是取反器(negator),綁定器用于綁定函數(shù)對象中的某個參數(shù),取反器用于將函數(shù)對象的返回值進行翻轉(zhuǎn),一般的,可以使用取反器的函數(shù)對象都是那些返回值為bool型的函數(shù)對象.
CGL中不能實現(xiàn)函數(shù)適配器,因為綁定器在綁定參數(shù)的時候是通過將所需要綁定的參數(shù)作為模版參數(shù)傳遞給函數(shù)對象類進行參數(shù)綁定的,如:
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函數(shù)的實現(xiàn),你可以看到這個函數(shù)是返回一個binder2nd的函數(shù)對象類,而所需要綁定的參數(shù)作為binder2nd的value參數(shù)存儲起來,在調(diào)用binder2nd的operator()時再使用value參數(shù),由于C中沒有辦法做到重載operator()函數(shù),所以不能實現(xiàn)binder.
同樣的,C中也沒有辦法實現(xiàn)negator函數(shù)對象適配器,見代碼:
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函數(shù)not1函數(shù)的實現(xiàn).可以看到這個函數(shù)返回一個unary_negate函數(shù)對象,而在這個函數(shù)對象重載的operator()中對pred函數(shù)的返回值進行取反以達到negator函數(shù)對象適配器的作用,同樣的,用于C中不能做到重載operator(),所以沒有辦法實現(xiàn)negator函數(shù)對象適配器.
STL中還有一些預(yù)定義的函數(shù)對象如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的函數(shù)參數(shù)是通過模版參數(shù)T傳入的,任何想要采用plus的類型都需要提供operator + 操作符.因為這個特點,C中也沒有辦法實現(xiàn)這些預(yù)定義的函數(shù)對象.
鑒于以上的原因,CGL中的"函數(shù)對象"被定義為幾種函數(shù)指針:
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)();


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