new functions min and max for 2 - 2+n parameters - now in C++11 vith variadics

This commit is contained in:
Marc Wäckerlin
2015-04-29 12:53:52 +00:00
parent 3a8795996f
commit d48ffc8e92
2 changed files with 266 additions and 218 deletions

View File

@@ -16,6 +16,7 @@
#include <string>
#include <iostream>
#include <algorithm>
#include <mrw/checkcxx11.hxx>
namespace mrw {
@@ -52,6 +53,11 @@ namespace mrw {
template<typename T> T& max(T& t1, T& t2) {
return t2<t1?t1:t2;
}
//! Get the maximum out of two values
template<typename T> const T& max(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
#ifdef MRW__OLD_PRE11_COMPILER
//! Get the maximum out of three values
template<typename T> T& max(T& t1, T& t2, T& t3) {
return max(t1, max(t2, t3));
@@ -68,10 +74,6 @@ namespace mrw {
template<typename T> T& max(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) {
return max(max(t1, t2, t3), max(t4, t5, t6));
}
//! Get the maximum out of two values
template<typename T> const T& max(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
//! Get the maximum out of three values
template<typename T> const T& max(const T& t1, const T& t2, const T& t3) {
return max(t1, max(t2, t3));
@@ -91,6 +93,17 @@ namespace mrw {
const T& t4, const T& t5, const T& t6) {
return max(max(t1, t2, t3), max(t4, t5, t6));
}
#else
//! Get the maximum out of any number of arguments
template<typename T, typename... T2> T& max(T& t1, T& t2, T2&... args) {
return max(t1, max(t2, args...));
}
//! Get the maximum out of any number of arguments
template<typename T, typename... T2> const T& max(const T& t1, const T& t2,
const T2&... args) {
return max(t1, max(t2, args...));
}
#endif
//@}
/** @defgroup getmin Get the Minimum of Values */
@@ -99,6 +112,11 @@ namespace mrw {
template<typename T> T& min(T& t1, T& t2) {
return t1<t2?t1:t2;
}
//! Get the minimum out of two values
template<typename T> const T& min(const T& t1, const T& t2) {
return t1<t2?t1:t2;
}
#ifdef MRW__OLD_PRE11_COMPILER
//! Get the minimum out of three values
template<typename T> T& min(T& t1, T& t2, T& t3) {
return min(t1, min(t2, t3));
@@ -115,10 +133,6 @@ namespace mrw {
template<typename T> T& min(T& t1, T& t2, T& t3, T& t4, T& t5, T& t6) {
return min(min(t1, t2, t3), min(t4, t5, t6));
}
//! Get the minimum out of two values
template<typename T> const T& min(const T& t1, const T& t2) {
return t2<t1?t1:t2;
}
//! Get the minimum out of three values
template<typename T> const T& min(const T& t1, const T& t2, const T& t3) {
return min(t1, min(t2, t3));
@@ -138,6 +152,17 @@ namespace mrw {
const T& t4, const T& t5, const T& t6) {
return min(min(t1, t2, t3), min(t4, t5, t6));
}
#else
//! Get the minimum out of any number of arguments
template<typename T, typename... T2> T& min(T& t1, T& t2, T2&... args) {
return min(t1, min(t2, args...));
}
//! Get the minimum out of any number of arguments
template<typename T, typename... T2> const T& min(const T& t1, const T& t2,
const T2&... args) {
return min(t1, min(t2, args...));
}
#endif
//@}
/** @brief Dual <code>?:</code> operation: