Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these C++ headers specified to include <initializer_list>?

As can be seen from https://stackoverflow.com/a/26614984/481267 the following headers are guaranteed by the standard to #include <initializer_list>:

  • Everything in [containers]
  • <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?

like image 971
Brian Bi Avatar asked Jun 06 '16 23:06

Brian Bi


People also ask

What is initializer_list?

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 .

What is initializer_list constructor?

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.


1 Answers

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.

like image 161
Bo Persson Avatar answered Oct 18 '22 02:10

Bo Persson