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};
}
1
  • 2
    In practice it's not ill-formed, because std::vector constructor uses initializer_list, so that must be included in <vector>. 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. Commented Jun 22, 2015 at 2:56

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

@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 <initializer_list>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.