Reputation: 9
One of the consequences of std::array
is that its implementation-defined data must be public or else it will no longer be an aggregate. This allows me to do stuff like this:
#include <array>
#include <iostream>
int main()
{
std::array<int, 5> arr{1, 2, 3, 4, 5};
#if defined(_LIBCPP_VERSION)
for (int i : arr.__elems_)
#else
for (int i : arr._M_elems)
#endif
std::cout << i;
}
This seems rather wonky to me, considering that most of the standard library is well encapsulated and protected from misuse. Is there any workaround to this? Could the interface of std::array
be improved?
Upvotes: 0
Views: 94
Reputation: 50101
The standard committee did fine by making your code illegal. You are using implementation reserved identifiers. If you are going to shoot yourself in the foot intentionally, then that is not the committee's problem.
In this special case the "misuse" does not even matter because you can access that data via the "official" interface anyways. But even if that was not the case, again: Just don't do stupid stuff on purpose.
Upvotes: 6