0

Despite the multiple questions about performance\benefits\properties of std::array vs std::vector or C style array, I still never got a clear answer about actual practical usage and types of problems that can be (and should be solved with std::array). the reason i am asking is because me myself and in all the projects that i was working in C++ i never saw any usage of std::array but most of the work done with std::vector, is some are cases we used std:deque.

After compering the properties seems like array is something in the middle that like a C style array that have a thin(not so thin - have a lot of APIs) layer of abstraction but not heavy as std::vector. feel like if performance really matter or i have limitation because of embedded device i would still use C style array.

I tried to compare some other relatively large C++ project for example Notepad++ source code, and they very seldom use std::array only in json.hpp which is third party of its own, so to summarize

  1. What are actual practical usage that can be solved by std::array better then other STL or native array?

  2. is it possible that modern C++ compiler can optimize away all the abstraction leaving it basically like native C array.

  3. I noticed in json.hpp that used by notepad++ source code that std::array used with char_traits and templates, is it possible that std::array can be used in implementation of other Libraries that are using templates and template meta-programming heavily while native C array would require more adapters or workaround that have performance penalty?

EDIT: I understand that std::array properties like fixed size and the sizes is known, but why it is still relatively seldom used.

Thanks a lot :)

4
  • Fixed size, automatically allocated array that you need to pass to or return from a function. Commented Apr 19, 2022 at 22:05
  • The obvious practical benefit over C-style array is that the array's size is available as part of it representation. Commented Apr 19, 2022 at 22:07
  • Ad. 2 - yes, that should be the case. std::array is defined as an aggregate, overhead should be minimal (if any) over C-style array. Unless you of course copy it by accident, because you replaced void foo(int* arr) with void foo(std::array<int, 5>) without any second thought. Commented Apr 19, 2022 at 22:07
  • Regarding 2, to these weak eyes, compilers do a pretty damn good job annihilating the extra wrappings around the array inside a std::array Commented Apr 19, 2022 at 22:07

1 Answer 1

2

This question is somewhat broad and seems likely to go down the opinionated route, still:

ad.1 std::array provides STL-like API for C-style array. Moreover it prevents the array to pointer decay. The latter is oftentimes desired, e.g. due to the fact that size information is preserved when passing it.

ad.2 As for a general case - yes, std::array is a simple thin wrapper around its C-style counterpart. For specific cases - passing std::array by value can cause copies, which won't be the case for C-style array as the simply cannot be passed by value. On the other hand, this also provides a more consistent interface. Generally - if you are concerned about performance - measure. I dare say, in most cases it won't matter at all and if a form of static array is needed, std::array should be a go-to type over the C-style one, see C++ core guidelines on that topic.

Ad.3 Not sure I follow. Again: what std::array is, is mostly a convenience wrapper around C-style one. If you're interested in the gory details, you can look up boost::array, it should be pretty close to the std one. True, using C-style arrays would probably require more tinkering or just writing sth equivalent to the std::array class template. As for potentially less performant workarounds, the main one I can think of is vector or some other form of dynamically allocated storage.

Note, I wrote "potentially": contemporary compilers are allowed to elide heap operations, so again - measure first, judge later.

Also, the array vs vector dilemma often goes down to fixed-size vs. resizable , i.e. the intent of the programmer. Of course there is a plethora of exceptions to this rule, but for a most general case I'd opt for being expressive first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.