vendredi 15 mai 2015

Why standard containers use function templates instead of Koenig operators

This question is inspired by Issue with std::reference_wrapper. Let' say, for example, operator< for std::vector. It's defined as a function template as

template< class T, class Alloc >
bool operator<( const vector<T,Alloc>& lhs,
                const vector<T,Alloc>& rhs );

As a result, implicit conversion of function argument to the type of the corresponding function parameter is denied. This greatly reduces the usefulness and convenience of std::reference_wrapper. For example, you cannot use std::sort or std::unique on std::vector<std::reference_wrapper<std::vector<int>>>.

On the other hand, all the problems are solved only if operator< is defined as a Koenig operator like

template <...>
class vector ... {
  friend bool operator<(const vector& a, const vector& b) {...}
};

I'm wondering why the standard library has adopted the former approach instead of this?

Aucun commentaire:

Enregistrer un commentaire