3

§[dcl.init.list] 8.5.4/2:

The template std::initializer_list is not predefined; if the header <initializer_list> is not included prior to a use of std::initializer_list — even an implicit use in which the type is not named (7.1.6.4) — the program is ill-formed.

Does that mean this program is ill-formed?

#include <vector>
int main() {
    // uses vector::vector(initializer_list<T>, const Allocator&) constructor
    std::vector<int> v = {1, 2, 3};
}
Praetorian
  • 106,671
  • 19
  • 240
  • 328
template boy
  • 10,230
  • 8
  • 61
  • 97
  • 2
    In practice it's not ill-formed, because `std::vector` constructor uses `initializer_list`, so that must be included in ``. But unless that dependency is stated somewhere, the formal could be ill-formed. And the reason for that is that the standard library is allowed to do any kinds of magic, including relying on non-standard behavior of the compiler, so that one can't reason that the standard library must be implemented in the same kind of way as one's own code. – Cheers and hth. - Alf Jun 22 '15 at 02:56

1 Answers1

5

Your program is not ill-formed because <vector> is guaranteed to include <initializer_list> (the same is true for all standard library containers)

§23.3.1 [sequences.general]

Header <vector> synopsis

#include <initializer_list>
...

Searching the standard for #include <initializer_list> reveals the header is included along with the following headers

  • <utility>
  • <string>
  • <array>
  • <deque>
  • <forward_list>
  • <list>
  • <vector>
  • <map>
  • <set>
  • <unordered_map>
  • <unordered_set>
  • <queue>
  • <stack>
  • <algorithm>
  • <random>
  • <valarray>
  • <regex>
Praetorian
  • 106,671
  • 19
  • 240
  • 328
  • 1
    cf. http://stackoverflow.com/questions/26614983/which-headers-in-the-c-standard-library-are-guaranteed-to-include-another-head – T.C. Jun 22 '15 at 03:23
  • @T.C. Ha! Turns out I even upvoted you on that answer :). I'll leave it to the OP to decide if it's a dupe, since this one is specifically about `` – Praetorian Jun 22 '15 at 03:27