3 ms·
> <initializer_list> is "built-in" You still need to include the header for any of the following: 1. Constructors (or functions) with `initializer_list` param
by smitherfield 9y ago
> <initializer_list> is "built-in"
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 http://en.cppreference.com/w/cpp/iterator#Iterator_adaptors
- saagarjha 9y agoBy "built-in", I was mostly talking about the fact that the literal syntax doesn't require a header. Case in point, here's some code I took straight from a toy AVL implementation I wrote a while back: 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).
- smitherfield 9y agoThat requires including the header. It's probably included in one of the other headers you included, but that's not required by the standard.
- makecheck 9y agoYeah, it really needs to become “built-in” though. 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>").