Difference between const std::array<T,N> and std::array<const T, N>

Solution 1:

Copies of std::array<const T, N> are still "logically const", whereas by default a copy of const std::array<T, N> is mutable. If allowing or preventing that matters to you, one is preferable to the other.

There are differences in what templates they match, e.g. the case in Ilya's answer.

Solution 2:

there could be at least one difference - case when you need to pass variable to some other function, for example:

#include <array>

std::array<const int, 5> cc_arr5 = {1,2,3,4,5};
const std::array<int, 5> cc_arr5_2 = {1,2,3,4,5};

template<class T, std::size_t N>
void func(std::array<T, N>& a) {
    // do not change a, for example just calculate sum
}

int main()
{
    func(cc_arr5);
    func(cc_arr5_2); // this line does not compile

    return 0;
};

Solution 3:

They are indeed different types, so you cannot use one in a place whether the other would be expected, for example in a function call or an assignment.

That being said, as an array is a fixed size container with no auxiliary data member, having the container const prevent any change to its elements and having the elements const does not allow any change to the container since it obviously has no other data member.

Said differently both types will have the very same valid and invalid operations.

The main difference I can think about, is the const_cast implication: an array can be (implicitly) converted to a const array of the same element type but not to an array of the const modified type:

std::array<int, 3> a {1, 2, 3};

std::array<const int, 3> b = a; // Error: no known conversion from 'array<int, [...]>' to 'const array<const int, [...]>
const std::array<int, 3> c = a; // Ok