Share via

C++ max function

Sid Kraft 46 Reputation points
2026-06-13T13:49:06.2166667+00:00

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

Developer technologies | C++
Developer technologies | C++

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.

0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-06-13T13:49:14.34+00:00

    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.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.