diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index e968ae8af..346225a05 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -246,8 +246,14 @@ template inline Vector min(const Vector T min(Corrade::Containers::ArrayView range) { + if(range.empty()) return {}; + T out(range[0]); for(std::size_t i = 1; i != range.size(); ++i) out = min(out, range[i]); @@ -287,8 +293,14 @@ template Vector max(const Vector& v return out; } -/** @brief Maximum of a range */ +/** +@brief Maximum of a range + +If the range is empty, returns default-constructed value. +*/ template T max(Corrade::Containers::ArrayView range) { + if(range.empty()) return {}; + T out(range[0]); for(std::size_t i = 1; i != range.size(); ++i) out = max(out, range[i]); diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index 1188146d0..24744e009 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -140,6 +140,8 @@ void FunctionsTest::minList() { CORRADE_COMPARE(Math::min({Vector3i(5, -3, 2), Vector3i(-2, 14, 7), Vector3i(9, -5, 18)}), Vector3i(-2, -5, 2)); + + CORRADE_COMPARE(Math::min(std::initializer_list{}), Vector3i{}); } void FunctionsTest::max() { @@ -153,6 +155,8 @@ void FunctionsTest::maxList() { CORRADE_COMPARE(Math::max({Vector3i(5, -3, 2), Vector3i(-2, 14, 7), Vector3i(9, -5, 18)}), Vector3i(9, 14, 18)); + + CORRADE_COMPARE(Math::max(std::initializer_list{}), Vector3i{}); } void FunctionsTest::minmax() {