C++ max function
Not sure about this one, Virtual Studio:
Works: max({SPANX,SPANY,SPANZ});
Does not work: max(SPANX,SPANY,SPANZ);, generates a "build" error
Why does one need the delimiters ({ )}; ?? Sid Kraft
2 answers
-
AI answer
std::maxis 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 nostd::maxoverload that takes three arguments, so the compiler reports a build error.max({SPANX, SPANY, SPANZ});passes a single argument of typestd::initializer_list<T>(constructed from the braced list), which matches themax(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 aninitializer_listso 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 overloadBut for three or more values, the
initializer_listform is required.
References:
AI-generated content may be incorrect. Read our transparency notes for more information. - Two arguments:
-
Taki Ly (WICLOUD CORPORATION) 2,145 Reputation points • Microsoft External Staff • Moderator
Hi @Sid Kraft ,
The reason you are seeing a build error with
max(SPANX, SPANY, SPANZ);but success withmax({SPANX, SPANY, SPANZ});comes down to how thestd::maxfunction is defined in the C++ Standard Library (<algorithm>). By default, the standardstd::maxfunction is only designed to compare exactly two arguments, or two arguments along with a custom comparison function. It does not have an overload that takes three or more individual template arguments directly.So, when you call
std::max(SPANX, SPANY, SPANZ);, the C++ compiler gets confused. It assumes you are trying to call this specific overload:std::max(const T& a, const T& b, Compare comp)It thinks
SPANXandSPANYare the two values to compare, andSPANZis a custom mathematical comparison function. SinceSPANZis just a variable (likely anint) and not a callable function, the build fails.Why does
{}fix it?Starting in C++11, a new overload was added to the standard library that accepts an
std::initializer_list. By wrapping your variables in delimited curly braces{ }, you are instantiating anstd::initializer_liston the fly. You are essentially passing one single argument (a list containing three elements) intostd::max, rather than three separate arguments. The function then iterates through that list and correctly returns the largest value.Official Documentation Reference:
You can verify these overloads in the official C++ references for
std::max:- Microsoft Learn documentation:
std::max(algorithm) - C++ Reference (
cppreference.com): std::max
If you look at the C++ Reference link above, you will see the exact signatures:
-
template< class T > constexpr const T& max( const T& a, const T& b );(Only compares 2 items) -
template< class T, class Compare > constexpr const T& max( const T& a, const T& b, Compare comp );(Compares 2 items using a custom comparator) -
template< class T > constexpr T max( std::initializer_list<T> ilist );(Compares entirely through a list using{})
This is why
max({SPANX, SPANY, SPANZ});works. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.Disclaimer: Some links are non-Microsoft website. The pages appear to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classifies as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.
Thank you.
-
Taki Ly (WICLOUD CORPORATION) 2,145 Reputation points • Microsoft External Staff • Moderator
Hi @Sid Kraft ,
Have you seen my answer in this post? If you have any question, feel free to reach out. I'm happy to support you.
Sign in to comment - Microsoft Learn documentation:
