Ich habe festgestellt, dass binary_function
aus C++ 11 entfernt wird. Ich frage mich warum.Warum wurde unary_function, binary_function, aus C++ 11 entfernt?
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;
};
MODIFIED ------------------- -------------------------------------------------- -------
Zum Beispiel, wenn wir unseren Adapter für die Funktion sogar in C + schreiben möchten +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
Wie können wir das in C++ 11 ohne unary_function
verbessern?
binary_function unsere Arbeit vereinfachen und Code konsistenter, richtig machen? –
camino
@camino: Kann es, obwohl? –
ohne es müssen wir typedef definieren T first_argument_type; ...., vielleicht werden wir etwas vergessen – camino