A min= idiom in C++?
c++, c++11, idioms, min, operator-overloading
Solution
It's certainly not idiomatic, but you might be able to use something called named operators (see these Q&As here and here, developed by @Yakk and @KonradRudolph), and write
x <min>= y;
which is made possible by overloading `operator<` and `operator>`, combined with a clever wrapped `named_operator`. The full code is given by the link above, but uses code like
template <typename T1, typename T2, typename F>
inline auto operator >(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
-> decltype(lhs.f(std::declval<T1>(), std::declval<T2>()))
{
return lhs.f(lhs.value, rhs);
}
Using `std::min` as template argument for the template parameter `F`, would update the lhs of the expression with the min of the lhs and rhs.
Problem
We use ``` x += y ``` instead of ``` x = x + y ``` And similarly for `*,/,-` and other operators. Well, what about ``` x min= y ``` instead of ``` x = std::min(x, y) ``` ? Is there a commonly-used idiom for this command, not requiring the (impossible) extension of the language with another operator?