by smitherfield · 8y ago · 0 comments
You still need to include the header for any of the following:
1. Constructors (or functions) with `initializer_list` parameters.
template<typename T> struct S {
vector<T> vec;
S(initializer_list<T> il)
: vec(il) {}
};
2. `initializer_list` literals in range-for loops.[1] for (int prime : {2, 3, 5, 7, 11})
// ...
3. Named `initializer_list` variables. const auto primes = {2, 3, 5, 7, 11};
const initializer_list<int> primes2 =
{13, 17, 19, 23, 29};
> <iterator> is generally subsumed by "auto" and type inference.The iterator adapters[2] are still pretty useful and (I thought?) commonly used.
Also, the "range access" and (new for C++17) "container access" free functions are very useful for writing template code that works with both C arrays and STL-style containers, but they're defined in many other headers.
[1] Useful with variadic templates:
[](auto&&... args) {
for (auto &&arg : {args...})
// ...
}
[2] http://en.cppreference.com/w/cpp/iterator#Iterator_adaptors auto children = {&grandparent->left, &grandparent->right};
auto child = elvt::find_if(children, [parent](auto &child) {
return *child == parent;
});
Yes, it's an abuse of initializer_list being a convenient "array-like" structure, but it does show that not every use of initializer_list needs to be used as you've mentioned. (In fact, I probably use initializer_lists more this way than in their "intended" way. I'm sure someone is going to yell at me some day for it, but nobody's done it yet).I’ve seen some really bizarre compiler errors that tell you very little (i.e. you dared to use curly braces somewhere and somehow from that you must figure out that you need a "#include <initializer_list>").