constexpr max(initializer_list) vs max_elementSection: 26.8.9 [alg.min.max] Status: C++17 Submitter: Marc Glisse Opened: 2014-02-21 Last modified: 2017-07-30
Priority: 3
View other active issues in [alg.min.max].
View all other issues in [alg.min.max].
View all issues with C++17 status.
Discussion:
As part of the resolution for LWG issue 2350, max(initializer_list) was marked as constexpr. Looking
at two implementations of this function (libstdc++ and libc++), both implement it in terms of max_element, which is not
marked as constexpr. This is inconsistent and forces some small amount of code duplication in the implementation. Unless we
remove constexpr from this overload of max, I believe we should add constexpr to max_element.
[2015-02 Cologne]
AM: Can we implement this with the C++14 constexpr rules? JM: Yes. AM: Ready? [Yes]
Proposed resolution:
This wording is relative to N3936.
In 26.1 [algorithms.general], header <algorithm> synopsis, and 26.8.9 [alg.min.max], change as
indicated (add constexpr to every signature from the first min_element to the second minmax_element)::
template<class ForwardIterator>
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
constexpr ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
Compare comp);
[…]
template<class ForwardIterator>
constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
constexpr ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
Compare comp);
[…]
template<class ForwardIterator>
constexpr pair<ForwardIterator, ForwardIterator>
minmax_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
constexpr pair<ForwardIterator, ForwardIterator>
minmax_element(ForwardIterator first, ForwardIterator last, Compare comp);