One way:
template<class T, size_t N>
using c_array = T[N];
template<class T, size_t N>
c_array<T, N>& as_c_array(std::array<T, N>& a) {
return reinterpret_cast<T(&)[N]>(*a.data());
}
int main() {
std::array<int, 2> a;
int(&b)[2] = as_c_array(a);
}
The standard requires that std::array
is an aggregate and it's only member is T[N]
, a pointer to which std::array::data()
returns. As the address of an aggregate coincides with the address of its first member, calling and dereferencing std::array::data()
is not strictly necessary, reinterpret_cast<T(&)[N]>(a)
works as well.
std::array
originated in boost, where its sole purpose was to provide a standard container interface (begin/end/size/empty/etc.
) to built-in arrays T[N]
and nothing else, so that it doesn't have any overhead, 0-cost abstraction. Hence, you can basically cast boost::array<T, N>
and T[N]
back and forth albeit possibly breaking aliasing rules by doing so (the compiler assumes that boost::array<T, N>
and T[N]
refer to different objects, so you need to know how to cope with that in your specific case).
The standard dropped all the rationale and expressed the requirements of std::array
in very weak and vague terms. So that people wonder whether it is truly only T[N]
member there and not some allegedly extra-terrestrial type that satisfies the requirement.