how convert std::array<char, N> to char (&dest)[N]?

北城余情 提交于 2019-12-05 08:38:54

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!