You could (emphasis on could, because I don't think it's standard) do something like this:
template <typename T> struct has_typedef_key_compare { // Types "yes" and "no" are guaranteed to have different sizes, // specifically sizeof(yes) == 1 and sizeof(no) == 2. typedef char yes[1]; typedef char no[2]; template <typename C> static yes& test(typename C::key_compare*); template <typename> static no& test(...); // If the "sizeof" of the result of calling test<T>(0) would be equal to sizeof(yes), // the first overload worked and T has a nested type named foobar. static const bool value = sizeof(test<T>(0)) == sizeof(yes); }; template <typename T> struct is_sorted_container { static const bool value; }; template <typename T> const bool is_sorted_container<T>::value = has_typedef_key_compare<T>::value;
The rationale is that all sorted stl containers (which I looked at) have a key_compare function. Of course, this is NOT a standard. This will NOT cover non-stl types. In general, I do not like it, but I thought that I would publish it as an interesting alternative.
How the stl function works (e.g. std :: upper_bound), this should put a sorted iterator constraint in the comments and let the end user understand this.
Ideahat
source share