[valarray.access]

# 29 Numerics library [[numerics]](./#numerics)

## 29.6 Numeric arrays [[numarray]](numarray#valarray.access)

### 29.6.2 Class template valarray [[template.valarray]](template.valarray#valarray.access)

#### 29.6.2.4 Element access [valarray.access]

[ð](#lib:operator%5b%5d,valarray)

`const T& operator[](size_t n) const;
T& operator[](size_t n);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7577)

*Hardened preconditions*: n < size() is true[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7581)

*Returns*: A reference to the corresponding element of the array[.](#2.sentence-1)

[*Note [1](#note-1)*:

The expression (a[i] = q, a[i]) == q evaluates to true for any non-constant valarray<T> a,
any T q, and for any size_t i such that the value of i is less than the length of a[.](#2.sentence-2)

â *end note*]

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7591)

*Remarks*: The expression addressof(a[i + j]) == addressof(a[i]) + j evaluates to true for all size_t i and size_t j such that i + j < a.size()[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7597)

The expression addressof(a[i]) != addressof(b[j]) evaluates to true for any two arraysa and b and for anysize_t i and size_t j such that i < a.size() and j < b.size()[.](#4.sentence-1)

[*Note [2](#note-2)*:

This property indicates an absence of aliasing and can be used to
advantage by optimizing compilers[.](#4.sentence-2)

Compilers can take advantage
of inlining, constant propagation, loop fusion,
tracking of pointers obtained fromoperator new,
and other techniques to generate efficientvalarrays[.](#4.sentence-3)

â *end note*]

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L7614)

The reference returned by the subscript operator for an array shall
be valid until the member function[resize(size_t, T)](valarray.members#lib:valarray,resize "29.6.2.8 Member functions [valarray.members]") is called for that array or until the lifetime of
that array ends, whichever happens first[.](#5.sentence-1)
