Compile time size of data member std::array in non-class template (C++14)

The standard provides a static version of array::size under the name tuple_size:

#include <array>
#include <tuple> // for std::tuple_size_v

static_assert(std::tuple_size<decltype(arr_)>::value == kAnotherArraySize, "");
static_assert(std::tuple_size_v<decltype(arr_)> == kAnotherArraySize); // C++17

You can create an instance of an array with the same type of Foo::arr_ within the static assertion:

class Foo {
    std::array<int, kArraySize> arr_;
    static_assert(decltype(arr_){}.size() == kAnotherArraySize, "");
};

See this example.

Note: this works only if the array value type is a POD or has a default constexpr constructor.


Just to offer another option, you can do partial specialization on template variables.

#include <array>
#include <cstddef>

// Defined/provided from elsewhere.
constexpr std::size_t kArraySize = 12U;
constexpr std::size_t kAnotherArraySize = 12U;

template <typename T>
constexpr std::size_t array_size = 0;

template <typename T, std::size_t N>
constexpr std::size_t array_size<std::array<T, N>> = N;

class Foo {
    std::array<int, kArraySize> arr_;
    static_assert(array_size<decltype(arr_)> == kAnotherArraySize, "");
};

int main() {}