Misplaced Pages

Array (C++): Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editContent deleted Content addedVisualWikitext
Revision as of 21:20, 17 April 2010 editIvansorokin (talk | contribs)50 editsNo edit summary← 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}} 
(58 intermediate revisions by 12 users not shown)
Line 1: Line 1:
#REDIRECT ]
A array is wrapper class that provide STL-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>.

Nicolai Josuttis introduced it under the name <tt>array</tt> in ] libraries. 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.

And several minor drawbacks:
* They do not provide STL-like interface.
* They do not made bound-checks even in debug builds.
* They have weird syntax.

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. That is impossible for object of any other standard type.

== Design ==

Array template class is defined in header <tt>&lt;array&gt;</tt> in C++ standard library and in header <tt>&lt;boost/array.hpp&gt;</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 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 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, you can initialize an array 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.
* <tt>array</tt> class unlike standard arrays can have zero-size.

== Links ==

*
*

Latest revision as of 11:58, 17 October 2021

Redirect to:

  • 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).