Namespaces
Variants
Views
Actions

Talk:cpp/container/array

From cppreference.com

[edit] Dubious Statement

From the page:

> This struct has the same aggregate type semantics as a C-style array. The size and efficiency of array<T,N> for some number of elements is equivalent to size and efficiency of the corresponding C-style array T[N].

The standard does not guarantee that std::array<T, N> is the same size as T[N]. It doesn't guarantee layout compatibility between std::array and C-style arrays. Check section 9.2; layout compatibility only exists between structs/classes, unions, and enumerators. It doesn't exist between structs and arrays.

Therefore, the standard does not guarantee that sizeof(std::array<T, N>) == sizeof(T[N]). Korval 03:54, 17 May 2013 (PDT)


I agree, the only thing the Standard guarantees is that the pointer returned by data() and the reference returned by operator[](size_t) (as well as at() and *begin()) refer to something that is "compatible" to a C-style raw array (i.e. contiguous storage, like vector).--91.14.111.187 05:17, 17 May 2013 (PDT)

The standard requires std::array<T,N> to be an aggregate with N elements, and that these N elements occupy contiguous storage. That's a very strong requirement, layout compatibility with T[N] is one of its consequences when T is a standard-layout type. Let's let a few more comments to be posted to that SO discussion and edit accordingly. --Cubbi 06:24, 17 May 2013 (PDT)
Edited the page, thank you for bringing this up. --Cubbi 10:12, 17 May 2013 (PDT)

[edit] Automatically Counting Initializer Elements

Are there any tricks (or future extensions in the standard to expect) that the size may be automatically determined from the number of initializing elements? IMHO this is the only disadvantage std::array has compared to native arrays.

Mwe (talk) 04:22, 18 February 2016 (PST)

std::experimental::make_array? T. Canens (talk) 05:43, 18 February 2016 (PST)