lucab0ni lucab0ni - 1 month ago 14
C++ Question

C++ Get min / max value of a static constexpr array at compile time

Let's say I have an array of integers defined like that:

static const int IntArray[] = {1, 5, 10, 12, 17};


Is there a way to the minimum or maximum value at compile time?

UPDATE:

Thanks to the comments to the answer it was me clear that the array has to be declared
constexpr
instead of
const
(that's was also my purpose). So, now IntArray is declared as follow:

static constexpr int IntArray[] = {1, 5, 10, 12, 17};

Answer

Note: this answer assumes that the array is actually constexpr -- if it is only const, its contents are not compile-time constants, and thus it is not possible at all.


Let's get the C++17 solution out of the way for future search-landers:

constexpr int IntArray[] = {1, 5, 10, 12, 17};
constexpr int min = *std::min_element(std::begin(IntArray), std::end(IntArray));
static_assert(min == 1);

C++11 is more picky with constexpr functions, so we have to roll out a recursive algorithm. This one is a simple, linear one:

template <class T>
constexpr T &constexpr_min(T &a, T &b) {
    return a > b ? b : a;
}

template <class T>
constexpr T &arrayMin_impl(T *begin, T *end) {
    return begin + 1 == end
        ? *begin
        : constexpr_min(*begin, arrayMin_impl(begin + 1, end));
}

template <class T, std::size_t N>
constexpr T &arrayMin(T(&arr)[N]) {
    return arrayMin_impl(arr, arr + N);
}

constexpr int IntArray[] = {1, 5, 10, 12, 17};
constexpr int min = arrayMin(IntArray);

See it live on Coliru