我發現從C++ 11中刪除了binary_function
。我想知道爲什麼。爲什麼從C++ 11中刪除unary_function,binary_function?
C++ 98:
template <class T> struct less : binary_function <T,T,bool> {
bool operator() (const T& x, const T& y) const {return x<y;}
};
C++ 11:
template <class T> struct less {
bool operator() (const T& x, const T& y) const {return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
改性 ------------------- -------------------------------------------------- -------
template<class arg,class result>
struct unary_function
{
typedef arg argument_type;
typedef result result_type;
};
例如,如果我們想寫我們的適配器功能,即使在C + +98,
template <class T> struct even : unary_function <T,bool> {
bool operator() (const T& x) const {return 0==x%2;}
};
find_if(bgn,end,even<int>()); //find even number
//adapter
template<typename adaptableFunction >
class unary_negate
{
private:
adaptableFunction fun_;
public:
typedef adaptableFunction::argument_type argument_type;
typedef adaptableFunction::result_type result_type;
unary_negate(const adaptableFunction &f):fun_(f){}
bool operator()(const argument_type&x)
{
return !fun(x);
}
}
find_if(bgn,end, unary_negate< even<int> >(even<int>())); //find odd number
怎樣才能不unary_function
改善這在C++ 11?
binary_function可以簡化我們的工作,使代碼更一致的,對不對? –
camino
@camino:可以嗎? –
沒有它,我們需要定義typedef T first_argument_type; ....,也許我們會忘記一些東西 – camino