2
0
mirror of https://github.com/boostorg/json.git synced 2026-01-19 04:12:14 +00:00
Files
json/doc/qbk/dom/init_lists.qbk

82 lines
2.3 KiB
Plaintext

[/
Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Official repository: https://github.com/boostorg/json
]
[/-----------------------------------------------------------------------------]
[section:initializer_lists Initializer Lists]
Initializer lists can be used to construct or
assign a __value__:
[snippet_init_list_1]
Simple initializer lists produce an __array__:
[snippet_init_list_2]
Initializer lists can be nested.
Here we construct an array as an element of an array:
[snippet_init_list_3]
When a two element initializer list is nested within
an enclosing initializer list, it is unclear whether
it represents an __array__ or an __object__:
[snippet_init_list_4]
In such cases, if every element consists
of a string followed by a single value, then the enclosing
initializer list is interpreted as an __object__.
Otherwise, it is interpreted as an __array__.
[snippet_init_list_5]
To resolve the ambiguity manually, use an explicit constructor:
[snippet_init_list_6]
Initializer lists can be used to unambiguously construct or
assign an __object__ or __array__:
[snippet_init_list_7]
Similarly, an initializer list for an __object__ is always
interpreted as an __object__. In such cases, the initializer list
must be a list of key-value pairs.
For example, the following code will not compile because
`1` is not convertible to a string:
```
object jo = { { 1, 0.39 }, { "venus", 0.72 }, { "earth", 1 } };
```
The requirement for an initializer list to be interpreted as an
__object__ or __array__ when initializing such an entity only
applies to the outermost initializer list; subsequent nested
elements will follow the usual ambiguity resolution rules.
[snippet_init_list_8]
Elements that are rvalues will be moved upon initialization:
[snippet_init_list_9]
[warning
Do not create variables of type __initializer_list__.
This may result in temporaries being destroyed
before the initializer list is used.
]
In all cases, the __storage_ptr__ owned by an __object__,
__array__, or __value__ constructed from an initializer list
will be propagated to each element, recursively.
[endsect]