Namespaces
Variants
Views
Actions

Talk:cpp/container/array

From cppreference.com

Contents

[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)

[edit] Double brace initialisation

The code example says double braces are required for initialisation in C++11 but this SO post contradicts that: https://stackoverflow.com/a/18792782/265521 156.67.241.66 00:59, 27 November 2017 (PST)

C++11 as published was rather unambiguous, only "declarations of the form T x = { a };" allowed brace elision. However, CWG 1270 from 2012 was a DR and therefore applies to C++11. We could edit the comment to say "prior to CWG 1270". --Cubbi (talk) 13:42, 27 November 2017 (PST)

[edit] Ambiguous sentences

> One should take note, however, that during swap, the iterator will continue to point to the same array element, and will thus change its value.

Whose value will be changed? It is not clear here. --Seeker_Liu (talk) 17:03 Monday, 27 July 2020 (UTC+8)