3

My question is pretty straightforward, but to be more specific I want to quote 2 lines from Stroustrup11.

  1. T[N] A fixed-size built-in array: N contiguous elements of type T; no size() or other member functions
  2. array<T,N> A fixed-size array of N contiguous elements of type T; like the built-in array, but with most problems solved

So what is the difference the author is mentioning? And what problems are solved for std::array<T,N> ?

Eduard Rostomyan
  • 6,736
  • 1
  • 29
  • 67

2 Answers2

5

The principal differences are that std::array<T, N> doesn't decay to a pointer to the first element where T[N] would, and you can take a value copy of a std::array<T, N>.

std::array also offers some useful functions, such as lexicographical comparison operators.

But because N has to be a compile time evaluable constant expression, std::vector<T> is often the preferred choice.

Bathsheba
  • 227,678
  • 33
  • 352
  • 470
2

Unlike built-in arrays, std::array objects can be copied and passed as function parameters.

Overloaded operators and member functions may supply extra debug and runtime checks and eliminate a lot of code duplication. Set of standard members simplifies use of std::array objects in generic code.

user7860670
  • 33,577
  • 4
  • 51
  • 80