• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::front
2
3```cpp
4reference front();
5const_reference front() const;
6```
7
8Returns a reference to the first element in the container. For a JSON container `#!cpp c`, the expression
9`#!cpp c.front()` is equivalent to `#!cpp *c.begin()`.
10
11## Return value
12
13In case of a structured type (array or object), a reference to the first element is returned. In case of number, string,
14boolean, or binary values, a reference to the value is returned.
15
16## Exceptions
17
18If the JSON value is `#!json null`, exception
19[`invalid_iterator.214`](../../home/exceptions.md#jsonexceptioninvalid_iterator214) is thrown.
20
21## Exception safety
22
23Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
24
25## Complexity
26
27Constant.
28
29## Note
30
31!!! danger
32
33    Calling `front` on an empty array or object is undefined behavior and is **guarded by an assertion**!
34
35## Example
36
37??? example
38
39    The following code shows an example for `front()`.
40
41    ```cpp
42    --8<-- "examples/front.cpp"
43    ```
44
45    Output:
46
47    ```json
48    --8<-- "examples/front.output"
49    ```
50
51## Version history
52
53- Added in version 1.0.0.
54- Adjusted code to return reference to binary values in version 3.8.0.
55