2

c++11 uses template to define the max_size of the array (eg. std::array<int, 5> a1;) but not constructor. (eg. std::array<int>(5) a1;)

Since template is going to generate code for the class, and if I have a lot of arrays just differs in sizes, there'll be a lot of code to be generated.

(1. It may cause increase in compile time. 2. It may cause the expension of the code part of the executable file.)

13
  • 4
    Because std::array wraps an array. It literally contains a T arr[N];. Commented Nov 30, 2012 at 8:07
  • 2
    You don't need to worry about generated code size - std::array is designed to be a perfect substitute for "normal" arrays, meaning although the compiler may have to generate some code multiple times, these are mostly optmimizable one-liners that will leave you with the same code as C-Arrays when Optimization is turned on. Commented Nov 30, 2012 at 8:15
  • 2
    @Xeo: Relevant: xkcd.com/725 Commented Nov 30, 2012 at 8:19
  • 2
    @Xeo: Also relevant... xkcd.com/1108 Commented Nov 30, 2012 at 8:19
  • 1
    @Mehrdad: Saw that one coming from a mile. :) Commented Nov 30, 2012 at 8:19

3 Answers 3

13

Because if it didn't, it wouldn't be able to be what it is.

std::array is an array. Not a dynamically-sized array. Not a runtime-sized array. It is an array, much like int arr[5].

C++ is a statically typed language, which means that C++ types must have a compile-time defined size. arr in the above example has a size; if you do sizeof(arr), you will get sizeof(int) * 5. sizeof(std::array<int, 5>) has a size as well, which is defined by the number of elements in it. Therefore, the size must be a compile-time defined quantity, since it factors into the compile-time defined size.

The differences between std::array and regular arrays are:

  • Arrays will decay into pointers implicitly. std::array does not; you need to explicitly call a function to do that.
  • Arrays are language arrays; std::array, to the language, is a struct which contains an array.

if I have a lot of arrays just differs in sizes, there'll be a lot of code to be generated.

Yes, you might. Then again... is this a serious concern? Have you really looked at a std::array implementation?

There's not much there. T operator[](int index) { return elems[index]; } I don't think getting a couple hundred instantiations of that function is going to be a problem. Same goes for begin, size, empty, etc. You're talking about code that will almost certainly be inlined.

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

5 Comments

"If you do sizeof(std::array<int, 5>), you'll get the same thing." -- Does the standard really guarantee that? Why can't std::array have trailing padding like any other class type?
@hvd Who said std::array can not have padding? std::array<char,3> will have (at least on most platforms)
@BЈовић Nicol did in this answer. "The same thing" refers to sizeof(int) * 5, does it not?
Thanks for editing, but it still doesn't look right: arr cannot have padding in int arr[5];. sizeof(int[5]) must be exactly sizeof(int) * 5. sizeof(std::array<int, 5>) can be larger.
I've read the assembly code generated and I know that now. Thanks!
7

std::array is meant as a thin wrapper over a fixed-sized array. For a dynamically-sized array, there is std::vector.

Comments

1

One benefit of std::array is that it allocates memory on the stack (unless you declare a global object) with minimal overhead.

If the array size was determined by a constructor parameter, allocation would have to be on the heap and in general the resulting object would be less efficient in terms of memory usage and performance.

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.