Skip to content

Make the Min/Max initializer_list overloads instantiable - #552

Open
karpovantonme wants to merge 1 commit into
devosoft:masterfrom
karpovantonme:fix/min-max-initializer-list
Open

Make the Min/Max initializer_list overloads instantiable#552
karpovantonme wants to merge 1 commit into
devosoft:masterfrom
karpovantonme:fix/min-max-initializer-list

Conversation

@karpovantonme

Copy link
Copy Markdown

Fixes #550.

std::initializer_list<const T &> is ill-formed, so the two list overloads of emp::Min and emp::Max could never be instantiated. Any call to them failed inside <initializer_list> itself:

initializer_list:63:12: error: '__begin_' declared as a pointer to a reference of type 'const int &'

with emp::Min({3, 1, 2}) as the whole test case.

The overloads now take std::initializer_list<T> and return by value. Returning a reference would dangle: the backing array of a braced list ends its lifetime at the end of the full expression that contains it, so a const T & into it is already gone by the time the caller reads it.

lst.size also gained the parentheses it was missing. As written it took the address of the member function, which is never null, so the assert could not fire.

Checked with c++ -std=c++20 -I include -fsyntax-only on a file calling both: it fails on master and compiles here.

std::initializer_list<const T&> is ill-formed, so any call to these two
failed to compile. Take std::initializer_list<T> 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 devosoft#550.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

emp::Min and emp::Max initializer_list overloads cannot be instantiated

1 participant