C++ max function

Sid Kraft 46 Reputation points

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

0 comments No comments

Sign in to comment

2 answers

  1. AI answer

    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:

    1. Two arguments: max(a, b)
    2. 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:

    AI-generated content may be incorrect. Read our transparency notes for more information.
  2. 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 with max({SPANX, SPANY, SPANZ}); comes down to how the std::max function is defined in the C++ Standard Library (<algorithm>). By default, the standard std::max function 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 SPANX and SPANY are the two values to compare, and SPANZ is a custom mathematical comparison function. Since SPANZ is just a variable (likely an int) 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 an std::initializer_list on the fly. You are essentially passing one single argument (a list containing three elements) into std::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:

    If you look at the C++ Reference link above, you will see the exact signatures:

    1. template< class T > constexpr const T& max( const T& a, const T& b ); (Only compares 2 items)
    2. template< class T, class Compare > constexpr const T& max( const T& a, const T& b, Compare comp ); (Compares 2 items using a custom comparator)
    3. 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.

    1. 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
Sign in to answer

Your answer