Search code examples
c++11stdarray

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


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?


Solution

  • 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.