Revision as of 21:48, 17 April 2010 editIvansorokin (talk | contribs)50 edits →Differences from standard containers← Previous edit | Latest revision as of 11:58, 17 October 2021 edit undoChristian75 (talk | contribs)Extended confirmed users, New page reviewers, Pending changes reviewers, Rollbackers114,301 edits {{R with history}} | ||
(50 intermediate revisions by 12 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT ] | |||
A array is wrapper class that provide ]-like interface to standard fixed-size C-arrays. It also overcome several limitations of standard array. | |||
{{R to section}} | |||
== Creation History == | |||
{{R with history}} | |||
In his book, ''Generic Programming and the STL'', Matthew H. Austern introduces a wrapper class for ordinary arrays with static size, called <tt>block</tt>. It is safer and has no worse performance than ordinary arrays. In ''The C++ Programming Language, 3rd edition'', ] introduces a similar class, called <tt>c_array</tt>, which Nicolai Josuttis present slightly modified in his book ''The C++ Standard Library - A Tutorial and Reference'', called <tt>carray</tt>. | |||
under the name <tt>array</tt> this class is introduced in ] libraries by Nicolai Josuttis. Later this class was introduced in C++ standard library in ]. | |||
== Motivation == | |||
Standard C arrays has several principal limitation: | |||
* They aren't ]s. They can not be copied like any other object. | |||
* They do not obey standard <tt>operator &</tt> semantics. | |||
* They do not provide STL-like interface. | |||
The second item means that in the following code | |||
<source lang="cpp"> | |||
int a; | |||
int * b1 = a; | |||
int * b2 = &a; | |||
</source> | |||
<tt>b1</tt> and <tt>b2</tt> receives the same value. This behavior differ from one of any other standard type. | |||
== Design == | |||
Array template class is defined in header <tt><array></tt> in C++ standard library and in header <tt><boost/array.hpp></tt> in boost. It can resides in namespaces <tt>std::</tt> (in C++0x), <tt>std::tr1::</tt> (in C++03 with TR1) or <tt>boost::</tt>. | |||
The <tt>array</tt> class template is parametrized with a type of element and a number of elements. It can be instantiated with any type that fulfills the <tt>CopyConstructible</tt> and <tt>Assignable</tt> requirements. It also itself fulfills <tt>CopyConstructible</tt> and <tt>Assignable</tt> requirements. | |||
If <tt>array</tt> class template is instantiated with a type that fulfills <tt>EqualityComparable</tt> or <tt>LessThanComparable</tt> requirements, it fulfills <tt>EqualityComparable</tt> or <tt>LessThanComparable</tt> correspondingly. | |||
Class also provides standard iterators and element access functions. | |||
=== Implementation as aggregate === | |||
<tt>array</tt> class is implemented as aggregate class. This allow array to be initialized with a brace-enclosing, comma-separated list of initializers for the elements of the container, written in increasing subscript order: | |||
<source lang="cpp"> | |||
array<int,4> a = { { 1, 2, 3 } }; | |||
</source> | |||
Note that if there are fewer elements in the initializer list, then each remaining element gets default-initialized (thus, it has a defined value). | |||
However, this approach has its drawbacks: passing no initializer list means that the elements have an indetermined initial value, because the rule says that aggregates may have: | |||
* No user-declared constructors. | |||
* No private or protected non-static data members. | |||
* No base classes. | |||
* No virtual functions. | |||
Note that for standard conforming compilers it is possible to use fewer braces (according to 8.5.1 (11) of the Standard). That is, array can be initialized as follows: | |||
<source lang="cpp"> | |||
array<int,4> a = { 1, 2, 3 }; | |||
</source> | |||
== Differences from standard array == | |||
* <tt>array</tt> class is value type. It satisfy <tt>CopyConstructable</tt> and <tt>Assignable</tt> requirements. | |||
* <tt>array</tt> class can not be implicitly casted to <tt>T *</tt> or <tt>T const *</tt>. However there is member function <tt>data()</tt> that returns pointer to first element. | |||
* <tt>array</tt> implementation is not required to do bound check. However implementation in boost do that for <tt>operator</tt>, but not for iterators. | |||
* Unlike standard arrays <tt>array</tt> class can have zero size. | |||
== Differences from standard containers == | |||
* <tt>array</tt> class do not provides constant-time swap. Instead it provides linear-time swap. | |||
* Because <tt>array</tt> class is aggregate it do not provides fill and range constructors. It default constructor also do not initializes elements to zero. | |||
* size() is always constant, based on the second template argument of the type. | |||
* The container provides no allocator support. | |||
== Links == | |||
* | |||
* |
Latest revision as of 11:58, 17 October 2021
Redirect to:
- To a section: This is a redirect from a topic that does not have its own page to a section of a page on the subject. For redirects to embedded anchors on a page, use {{R to anchor}} instead.
- With history: This is a redirect from a page containing substantive page history. This page is kept as a redirect to preserve its former content and attributions. Please do not remove the tag that generates this text (unless the need to recreate content on this page has been demonstrated), nor delete this page.
- This template should not be used for redirects having some edit history but no meaningful content in their previous versions, nor for redirects created as a result of a page merge (use {{R from merge}} instead), nor for redirects from a title that forms a historic part of Misplaced Pages (use {{R with old history}} instead).