• No results found

Arrays

In document C++ CLI Standard pdf (Page 124-126)

An Array is a data structure that contains a number of variables, which are accessed through computed indices. The variables contained in an Array, also called the elements of the Array, are all of the same type, and this type is called the element type of the Array.

An Array in C++/CLI differs from a native Array (§8.3.4) in that the former is allocated on the CLI heap, 5

and can have a rank other than one. The rank determines the number of indices associated with each Array element. The rank of an Array is also referred to as the dimensions of the Array. An Array with a rank of one is called a single-dimensional Array, and an Array with a rank greater than one is called a multi- dimensional Array.

Throughout this Standard, the term Array is used to mean an array in C++/CLI. A C++-style array is 10

referred to as a native array or, more simply, array, whenever the distinction is needed.

Each dimension of an Array has an associated length, which is an integral number greater than or equal to zero. The dimension lengths are not part of the type of the Array, but, rather, are established when an instance of the Array type is created at run-time. The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices can range from 0 to N – 1, inclusive. The 15

total number of elements in an Array is the product of the lengths of each dimension in the Array. If one or more of the dimensions of an Array have a length of zero, the Array is said to be empty.

The element type of an Array can be any type, including an Array type.

23.1 Array types

An Array type is declared using a pseudo-template ref class with the following declaration: 20

namespace cli {

template<typename T, int rank = 1> ref class array : Array {

}; }

25

The class is a pseudo-template because aspects of an Array type cannot be implemented in a library using the facilities of the language. An array-type is any specialization of the cli::array pseudo-template

class. For example:

array<int>^ arr1D = gcnew array<int>(10);

array<int, 3>^ arr3D = gcnew array<int, 3>(10, 20, 30);

30

23.1.1 The System::Array type

The System::Array type is the abstract base type of all Array types. An implicit reference conversion

(§??) exists from any Array type to System::Array, and an explicit reference conversion (§??) exists from System::Array to any Array type. Note that System::Array is not itself an array-type. Rather, it is a

reference-class-type from which all array-type are derived. 35

Is reference conversion the correct term? [[#118]]

23.2 Array creation

Array instances are created by array-creation-expressions (§??) or by field or local variable declarations that include an array-initializer (§23.6).

When an Array instance is created, the rank and length of each dimension are established and then remain 40

Arrays

An Array instance created by an array-creation-expression is always of an Array type. The

System::Array type is an abstract type, so it cannot be instantiated.

Elements of Arrays created by array-creation-expressions are always initialized to their default value (§??).

23.3 Array element access

Array elements are accessed using element-access expressions (§??) of the form A[I1, I2, …, IN],

5

where A is an expression having an Array type, and each IX is an expression of integral type or a type that

can be implicitly converted to an integral type.

An element-access expression differs from subscript expressions in Standard C++ (§5.2.1) in that in the former case, commas are not treated as operators. Rather, commas separate individual expressions that respectively match the dimension of the Array being accessed. However, parentheses can be used to force 10

the use of the comma operator in an expression. The result of an Array element-access is a variable, namely the Array element selected by the indices. Add examples. [[Ed]]

The elements of an Array can be enumerated using a for each statement (§16.2.1).

23.4 Array members

Every Array type inherits the members declared by the type System::Array. In addition, Arrays have

15

iterators compatible with Standard C++’s template library. Provide details for Array members. [[#73]]

23.5 Array covariance

For any two types A and B, if an implicit reference conversion (§??) or explicit reference conversion (§??)

exists from A to B, then the same reference conversion also exists from the Array type array<A, R> to the 20

Array type array<B, R>, where R is any given rank-specifier (but is the same for both Array types). This relationship is known as array covariance. In particular, Array covariance means that a value of an Array type array<A, R> might actually be a reference to an instance of an Array type array<B, R>, provided an implicit reference conversion exists from B to A.

Because of Array covariance, assignments to Arrays where the elements are ref classes will include a run- 25

time check, which ensures that the value being assigned to the Array element is actually of a permitted type (§??).

Array covariance does not extend to boxing conversions. For example, no conversion exists that permits an

array<int> to be treated as an array<Object^> or array<int^>.

Array covariance really only applies to handles of Arrays, not direct Arrays – in other words, do Arrays 30

have copy constructors? [[#74]]

23.6 Array initializers

In document C++ CLI Standard pdf (Page 124-126)