Search code examples
c++templatesvariadic-templatesc++17fold-expression

Can I implement max(A, max(B, max(C, D))) using fold expressions?


While trying to play around with C++17 fold expressions, I tried to implement max sizeof where result is maximum of the sizeof of types. I have an ugly fold version that uses variable and a lambda, but I am unable to think of a way to use fold expressions and std::max() to get the same result.

This is my fold version:

template<typename... T>
constexpr size_t max_sizeof(){
    size_t max=0;
    auto update_max = [&max](const size_t& size) {if (max<size) max=size; };
    (update_max(sizeof (T)), ...);
    return max;
}


static_assert(max_sizeof<int, char, double, short>() == 8);
static_assert(max_sizeof<char, float>() == sizeof(float));
static_assert(max_sizeof<int, char>() == 4);

I would like to write equivalent function using fold expressions and std::max(). For example for 3 elements it should expand to

return std::max(sizeof (A), std::max(sizeof(B), sizeof (C)));

Is it possible to do that?


Solution

  • Since nobody posted this one as an answer yet, the easiest way to do this with minimal effort is to just use the overload of std::max() that is ready-made for this problem: the one that takes an initializer_list:

    template<typename... T>
    constexpr size_t max_sizeof() {
        return std::max({sizeof(T)...});
    }