I saw the following statement in a code:
std::min(0);
The closest definition I can find is:
template <class T> constexpr T min (initializer_list<T> il);
So far I found the implementation in stl_algo.h
:
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline _Tp
min(initializer_list<_Tp> __l)
{ return *std::min_element(__l.begin(), __l.end()); }
I used to think that std::min()
only calculates the minimum of two items. Can anyone explain to me what does it actually do? And what is an initializer_list
?
To answer the question in your title, the minimum of a set of numbers when the set contains one number is the number itself. That's easy for me to understand. I hope it is for you to understand too.
I used to think that std::min() only calculates the minimum of two items.
It used to be but since C++11, it has changed. Since C++11, there is
template< class T >
T min( std::initializer_list<T> ilist );
and a few variants. It can be used as:
std::min({num1, num2, ..., numn});
I am surprised that
std::min(0);
works for you. It should not. Perhaps it is supported by g++ as a non-standard extension. However, you should be able to use
std::min({0}); // That becomes an intializer_list<int> with one item.
That is standards compliant.