Comparing std::minmax against a pair
std::minmax
has an initializer_list
overload. This returns a non-const
non-reference pair:
static_assert(std::minmax({2, 1}) == std::make_pair(1, 2));
Unfortunately this may be less performant, since the complexities respectively are "exactly one comparison" and "at most (3/2) * t.size()
applications of the corresponding predicate".
One thing you could do is take advantage of the std::minmax
overload that takes a std::initializer_list<T>
and returns a std::pair<T,T>
. Using that you could have
int main()
{
const int a = 10, b = 20;
static_assert(std::minmax({2, 1}) == std::make_pair(1, 2));
static_assert(std::minmax({a, b}) == std::make_pair(a, b));
}
Which will compile and allows you to get rid of make_cref_pair
. It does call std::minmax_element
so I am not sure if this decreases the efficiency or not.
One option is to explicitly convert the left-hand side to std::pair<int,int>
:
#include <algorithm>
#include <utility>
template <typename T1, typename T2>
constexpr std::pair<T1,T2> myminmax(const T1& t1, const T2& t2)
{
return std::minmax(t1,t2);
}
int main()
{
static_assert(myminmax(2, 1) == std::make_pair(1, 2));
}