fork(7) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <type_traits>
  4.  
  5.  
  6. template<typename T>
  7. T vmin(T&&t)
  8. {
  9. return std::forward<T>(t);
  10. }
  11.  
  12. template<typename T0, typename T1, typename... Ts>
  13. typename std::common_type<
  14. T0, T1, Ts...
  15. >::type vmin(T0&& val1, T1&& val2, Ts&&... vs)
  16. {
  17. if (val1 < val2)
  18. return vmin(val1, std::forward<Ts>(vs)...);
  19. else
  20. return vmin(val2, std::forward<Ts>(vs)...);
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26. int x = 7;
  27. int y = 3;
  28. int& z0 = x;
  29. int& z1 = y;
  30. ((z0<z1)?z0:z1)=2;
  31. // vmin(z0, z1) = 2; -- want this to work, return an lvalue, but cannot with common_type
  32. std::cout << x << "," << y << "\n";
  33. std::cout << vmin(3, 2, 0.9, 2, 5) << std::endl;
  34.  
  35. std::cout << vmin(3., 1.2, 1.3, 2., 5.2) << std::endl;
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
7,2
0.9
1.2