Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C++11 std::array a struct rather than a class?

Tags:

c++11

stdarray

Out of curiosity, I had a look at the LLVM implementation of std::array, and noticed that it is a struct. Most other STL containers I've looked at (vector, queue, map) are classes. And it appears in the standard as a struct so is intentional.

Anyone know why this might be?

like image 478
elSnape Avatar asked Dec 20 '22 23:12

elSnape


1 Answers

Technically, it's neither a struct nor a class -- it's a template.

std::array is required to be an aggregate. To make a long story short, this ends up meaning that it can't have anything private -- so it might as well be written as a struct (which defaults to making everything public) instead of a class, (which defaults to making everything private).

If you wanted to you could write it as a class anyway:

template <...>
class array {
public:
// ...

But you need to make everything public anyway, so you might as well use a struct that does that by default.

like image 118
Jerry Coffin Avatar answered Feb 27 '23 13:02

Jerry Coffin