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.)
Because if it didn’t, it wouldn’t be able to be what it is.
std::arrayis an array. Not a dynamically-sized array. Not a runtime-sized array. It is an array, much likeint arr[5].C++ is a statically typed language, which means that C++ types must have a compile-time defined size.
arrin the above example has a size; if you dosizeof(arr), you will getsizeof(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::arrayand regular arrays are:std::arraydoes not; you need to explicitly call a function to do that.std::array, to the language, is a struct which contains an array.Yes, you might. Then again… is this a serious concern? Have you really looked at a
std::arrayimplementation?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 forbegin,size,empty, etc. You’re talking about code that will almost certainly be inlined.