As can be seen from https://stackoverflow.com/a/26614984/481267 the following headers are guaranteed by the standard to #include <initializer_list>
:
<utility>
<string>
<algorithm>
<random>
<valarray>
<regex>
Most of these headers declare at least one function that takes a std::initializer_list<E>
argument, so it makes sense. However,
<array>
, <stack>
, and <queue>
have no such functions, although perhaps it makes sense to treat all containers uniformly here.<utility>
has no such functions.<iterator>
does have functions with an initializer_list
argument (rbegin
, rend
) but it's not specified to include <initializer_list>
.What is the rationale behind these exceptions?
An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .
std::initializer_list initializer_list objects are automatically constructed as if an array of elements of type T was allocated, with each of the elements in the list being copy-initialized to its corresponding element in the array, using any necessary non-narrowing implicit conversions.
Seems like there is no explicit rationale, just that some proposals for additions to the standard were made and those proposals were accepted.
At the very end of the document N2672 Initializer List proposed wording it just says:
In 20.2 Utility components [utility] paragraph 1:
This subclause contains some basic function and class templates that are used throughout the rest of the library.
Header
<utility>
synopsis#include<initializer_list> namespace std {
So, the authors of the paper saw initializer_list
as a utility, and so it ought to be included with the <utility>
header. And therefore it is.
The paper didn't propose any changes to the <iterator>
header, so none were made.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With