Search code examples
c++arraysc++11stlstdarray

Does a raw array have any advantages over a std::array?


According to the accepted answer on this question about raw arrays vs std::vector, the advantages of a raw array (back in 2010) were:

  • arrays are slightly more compact: the size is implicit
  • arrays are non-resizable; sometimes this is desireable
  • arrays don't require parsing extra STL headers (compile time)
  • it can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using)
  • fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed

To the best of my knowledge, std::array addresses all but the third point.

So unless I desperately need to improve my compile times, is there any reason to use a raw array over a std::array in C++11?


Solution

  • Yes, it doesn't require you to explicitly specify the size, which makes it easier to initialize it by hand:

    char const *messages[] =
    {
        "Hi",
        "Bye",
        "foo",
        "bar"
    };