Search code examples
c++c++11initializer-list

Implicit use of initializer_list


§[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};
}

Solution

  • 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>