#include <iostream>
#include <utility>
#include <type_traits>
template<typename T>
T vmin(T&&t)
{
return std::forward<T>(t);
}
template<typename T0, typename T1, typename... Ts>
typename std::common_type<
T0, T1, Ts...
>::type vmin(T0&& val1, T1&& val2, Ts&&... vs)
{
if (val1 < val2)
return vmin(val1, std::forward<Ts>(vs)...);
else
return vmin(val2, std::forward<Ts>(vs)...);
}
int main()
{
int x = 7;
int y = 3;
int& z0 = x;
int& z1 = y;
((z0<z1)?z0:z1)=2;
// vmin(z0, z1) = 2; -- want this to work, return an lvalue, but cannot with common_type
std::cout << x << "," << y << "\n";
std::cout << vmin(3, 2, 0.9, 2, 5) << std::endl;
std::cout << vmin(3., 1.2, 1.3, 2., 5.2) << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dXRpbGl0eT4KI2luY2x1ZGUgPHR5cGVfdHJhaXRzPgoKCnRlbXBsYXRlPHR5cGVuYW1lIFQ+ClQgdm1pbihUJiZ0KQp7CglyZXR1cm4gc3RkOjpmb3J3YXJkPFQ+KHQpOwp9Cgp0ZW1wbGF0ZTx0eXBlbmFtZSBUMCwgdHlwZW5hbWUgVDEsIHR5cGVuYW1lLi4uIFRzPgp0eXBlbmFtZSBzdGQ6OmNvbW1vbl90eXBlPAoJVDAsIFQxLCBUcy4uLgo+Ojp0eXBlIHZtaW4oVDAmJiB2YWwxLCBUMSYmIHZhbDIsIFRzJiYuLi4gdnMpCnsKCWlmICh2YWwxIDwgdmFsMikKCQlyZXR1cm4gdm1pbih2YWwxLCBzdGQ6OmZvcndhcmQ8VHM+KHZzKS4uLik7CgllbHNlCgkJcmV0dXJuIHZtaW4odmFsMiwgc3RkOjpmb3J3YXJkPFRzPih2cykuLi4pOwp9CgoKaW50IG1haW4oKQp7CglpbnQgeCA9IDc7CglpbnQgeSA9IDM7CglpbnQmIHowID0geDsKCWludCYgejEgPSB5OwoJKCh6MDx6MSk/ejA6ejEpPTI7Ci8vIAl2bWluKHowLCB6MSkg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