1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
The bf(C) language defines the i(initializer list) as a list of values enclosed
by curly braces, possibly themselves containing initializer lists. In bf(C)
these initializer lists are commonly used to initialize arrays and structs.
bf(C++) extends this concept by introducing the em(type)
tt(initializer_list<Type>) where tt(Type) is replaced by the type name of
the values used in the initializer list. Initializer lists in bf(C++) are,
like their counterparts in bf(C), recursive, so they can also be used with
multi-dimensional arrays, structs and classes.
Before using the tt(initializer_list) the tthi(initializer_list) header file
must be included.
Like in bf(C), initializer lists consist of a list of values surrounded by
curly braces. But unlike bf(C), em(functions) can define initializer list
parameters. E.g.,
verb( void values(std::initializer_list<int> iniValues)
{
})
A function like tt(values) could be called as follows:
verb( values({2, 3, 5, 7, 11, 13});)
The initializer list appears as an argument which is a list of values
surrounded by curly braces. Due to the recursive nature of initializer lists a
two-dimensional series of values can also be passes, as shown in the next
example:
verb( void values2(std::initializer_list<std::initializer_list<int>> iniValues)
{}
values2({{1, 2}, {2, 3}, {3, 5}, {4, 7}, {5, 11}, {6, 13}});)
Initializer lists are constant expressions and cannot be
modified. However, their em(size) and values may be retrieved using their
tt(size, begin), and tt(end) members as follows:
verb( void values(initializer_list<int> iniValues)
{
cout << "Initializer list having " << iniValues.size() << "values\n";
for
(
initializer_list<int>::const_iterator begin = iniValues.begin();
begin != iniValues.end();
++begin
)
cout << "Value: " << *begin << '\n';
})
Initializer lists can also be used to initialize objects of classes
(cf. section ref(UNIFORMINIT), which also summarizes the facilities of
initializer lists).
em(Implicit conversions), also called em(narrowing conversions)
hi(narrowing conversion) are not allowed when specifying values of
initializer lists. Narrowing conversions are encountered when values are used
of a type whose range is larger than the type specified when defining the
initializer list. For example
itemization(
it() specifying tt(float) or tt(double) values to define initializer
lists of tt(int) values;
it() specifying integral values exceeding the range of tt(float) to
define initializer lists of tt(float) values;
it() specifying values of integral types of a wider range than the
integral type that is specified for the initializer list, except if
the specified values lie within the range of the initializer list's
integral type
)
Some examples:
verb( initializer_list<int> ii{ 1.2 }; // 1.2 isn't an int value
initializer_list<unsigned> iu{ ~0ULL }; // unsigned long long doesn't fit
)
|