From 7582e316d5f651295968bde667f4f9f7dae11c5f Mon Sep 17 00:00:00 2001 From: Anton Karpov Date: Mon, 3 Aug 2026 18:51:32 +0300 Subject: [PATCH] Make the Min/Max initializer_list overloads instantiable std::initializer_list is ill-formed, so any call to these two failed to compile. Take std::initializer_list and return by value, since the backing array of a braced list dies at the end of the full expression. Also add the missing parentheses on lst.size(). Reported in #550. --- include/emp/math/math.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/include/emp/math/math.hpp b/include/emp/math/math.hpp index a275ee417..d995d9378 100644 --- a/include/emp/math/math.hpp +++ b/include/emp/math/math.hpp @@ -302,9 +302,11 @@ namespace emp { } /// A version of Min that allows a variable number of inputs to be compared. + /// Returns by value: the backing array of a braced list ends its lifetime at + /// the end of the full expression, so a reference into it would dangle. template - const T & Min(std::initializer_list lst) { - emp_assert(lst.size > 0); // Nothing to return if nothing in the list! + T Min(std::initializer_list lst) { + emp_assert(lst.size() > 0); // Nothing to return if nothing in the list! auto min_found = lst.begin(); for (auto it = lst.begin() + 1; it < lst.end(); it++) { if (*it < *min_found) { min_found = it; } @@ -313,9 +315,10 @@ namespace emp { } /// A version of Max that allows a variable number of inputs to be compared. + /// Returns by value, for the same reason as Min above. template - const T & Max(std::initializer_list lst) { - emp_assert(lst.size > 0); // Nothing to return if nothing in the list! + T Max(std::initializer_list lst) { + emp_assert(lst.size() > 0); // Nothing to return if nothing in the list! auto max_found = lst.begin(); for (auto it = lst.begin() + 1; it < lst.end(); it++) { if (*it > *max_found) { max_found = it; }