A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
std::max is overloaded for:
template<class Type>
constexpr Type& max(const Type& left, const Type& right);
template<class Type>
constexpr Type& max(initializer_list<Type> ilist);
So there are two relevant forms:
- Two arguments:
max(a, b) - An
initializer_list:max({a, b, c, ...})
max(SPANX, SPANY, SPANZ); tries to call a function taking three separate parameters, but there is no std::max overload that takes three arguments, so the compiler reports a build error.
max({SPANX, SPANY, SPANZ}); passes a single argument of type std::initializer_list<T> (constructed from the braced list), which matches the max(initializer_list<Type>) overload. That overload is specifically designed to take an arbitrary number of values and return the largest.
The braces { ... } are therefore required to form an initializer_list so that the correct overload can be called when using more than two values.
If only two values are needed, both forms are valid:
max(SPANX, SPANY); // two-argument overload
max({SPANX, SPANY}); // initializer_list overload
But for three or more values, the initializer_list form is required.
References: